Remove elements to make array sorted
Last Updated :
25 Mar, 2023
Given an array of integers, the task is to remove elements from the array to make the array sorted. That is, remove the elements which do not follow an increasing order.
Examples:
Input: arr[] = {1, 2, 4, 3, 5, 7, 8, 6, 9, 10}
Output: 1 2 4 5 7 8 9 10
Input: arr[] = {10, 12, 9, 5, 2, 13, 14}
Output: 10 12 13 14
Approach: Traverse the given array and for every element which is greater than or equal to the previously taken element, add this element to another array else skip to the next element. In the end, the newly created array will be sorted according to Stalin sort.
- For each element check if the element is greater than the previous element or not.
- If yes then check for the next element.
- Else remove that element.
- After all the elements have been traversed, we will get a sorted array.
Algorithm:
Step 1: Initialize an empty array brr[] of size n and a variable i to 1 to keep track of the index of the sorted array.
Step 2: Set the first element of brr[] to the first element of arr[].
Step 3: Loop through the array arr from index 1 to n-1.
a. If the current element at index i is greater than or equal to the last element of the sorted array brr[i-1],then insert the element at index i to the sorted array brr at index i and increment l by 1.
Step 4: Print the sorted array brr from index 0 to i-1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void removeElements( int arr[], int n)
{
int brr[n], l = 1;
brr[0] = arr[0];
for ( int i = 1; i < n; i++) {
if (brr[l - 1] <= arr[i]) {
brr[l] = arr[i];
l++;
}
}
for ( int i = 0; i < l; i++)
cout << brr[i] << " " ;
}
int main()
{
int arr[] = { 10, 12, 9, 10, 2, 13, 14 };
int n = sizeof (arr) / sizeof (arr[0]);
removeElements(arr, n);
return 0;
}
|
Java
class GFG
{
static void removeElements( int []arr, int n)
{
int []brr = new int [n];
int l = 1 ;
brr[ 0 ] = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
{
if (brr[l - 1 ] <= arr[i])
{
brr[l] = arr[i];
l++;
}
}
for ( int i = 0 ; i < l; i++)
System.out.print(brr[i] + " " );
}
static public void main (String[] args)
{
int []arr = { 10 , 12 , 9 , 10 , 2 , 13 , 14 };
int n = arr.length;
removeElements(arr, n);
}
}
|
Python3
def removeElements(arr, n) :
brr = [ 0 ] * n; l = 1 ;
brr[ 0 ] = arr[ 0 ];
for i in range ( 1 ,n) :
if (brr[l - 1 ] < = arr[i]) :
brr[l] = arr[i];
l + = 1 ;
for i in range (l) :
print (brr[i],end = " " );
if __name__ = = "__main__" :
arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ];
n = len (arr);
removeElements(arr, n);
|
C#
using System;
class GFG
{
static void removeElements( int []arr, int n)
{
int []brr = new int [n];
int l = 1;
brr[0] = arr[0];
for ( int i = 1; i < n; i++)
{
if (brr[l - 1] <= arr[i])
{
brr[l] = arr[i];
l++;
}
}
for ( int i = 0; i < l; i++)
Console.Write(brr[i] + " " );
}
static public void Main ()
{
int []arr = { 10, 12, 9, 10, 2, 13, 14 };
int n = arr.Length;
removeElements(arr, n);
}
}
|
Javascript
<script>
function removeElements(arr, n)
{
let brr = new Array(n);
brr.fill(0);
let l = 1;
brr[0] = arr[0];
for (let i = 1; i < n; i++)
{
if (brr[l - 1] <= arr[i])
{
brr[l] = arr[i];
l++;
}
}
for (let i = 0; i < l; i++)
document.write(brr[i] + " " );
}
let arr = [ 10, 12, 9, 10, 2, 13, 14 ];
let n = arr.length;
removeElements(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Algorithm:
Step 1: Initialize a variable i to 1 to keep track of the index of the sorted array.
Step 2: Loop through the array arr from index 1 to n-1.
a. If the current element at index i is greater than or equal to the last element of the sorted array arr[i-1], then insert the element at index i to the sorted array arr at index l and increment i by 1.
Step 3: Print the sorted array arr from index 0 to i-1.
Method 2: Instead of using an extra array, store them in the same array
C++
#include <bits/stdc++.h>
using namespace std;
void removeElements( int arr[], int n)
{
int l = 1;
for ( int i = 1; i < n; i++) {
if (arr[l - 1] <= arr[i]) {
arr[l] = arr[i];
l++;
}
}
for ( int i = 0; i < l; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 10, 12, 9, 10, 2, 13, 14 };
int n = sizeof (arr) / sizeof (arr[0]);
removeElements(arr, n);
return 0;
}
|
Java
class GFG{
static void removeElements( int arr[], int n)
{
int l = 1 ;
for ( int i = 1 ; i < n; i++)
{
if (arr[l - 1 ] <= arr[i])
{
arr[l] = arr[i];
l++;
}
}
for ( int i = 0 ; i < l; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 10 , 12 , 9 , 10 , 2 , 13 , 14 };
int n = arr.length;
removeElements(arr, n);
}
}
|
Python3
def removeElements(arr, n):
l = 1
for i in range ( 1 , n):
if (arr[l - 1 ] < = arr[i]):
arr[l] = arr[i]
l + = 1
for i in range (l):
print (arr[i], end = " " )
if __name__ = = "__main__" :
arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ]
n = len (arr)
removeElements(arr, n)
|
C#
using System;
class GFG{
public static void removeElements( int []arr, int n)
{
int l = 1;
for ( int i = 1; i < n; i++)
{
if (arr[l - 1] <= arr[i])
{
arr[l] = arr[i];
l++;
}
}
for ( int i = 0; i < l; i++)
Console.Write( arr[i] + " " );
}
public static void Main( string []args)
{
int []arr = { 10, 12, 9, 10, 2, 13, 14 };
int n = arr.Length;
removeElements(arr, n);
}
}
|
Javascript
<script>
function removeElements(arr, n)
{
let l = 1;
for (let i = 1; i < n; i++) {
if (arr[l - 1] <= arr[i]) {
arr[l] = arr[i];
l++;
}
}
for (let i = 0; i < l; i++)
document.write(arr[i] + " " );
}
let arr = [ 10, 12, 9, 10, 2, 13, 14 ];
let n = arr.length;
removeElements(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...