Remove elements to make array sorted

Given an array of integers, the task is to remove elements from the array to make 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 next element. In the end, the newly created array will be sorted according to Stalin sort.

1. For each element check if the element is greater than the previous element or not.
• If yes then check for next element.
• Else remove that element.
2. After all the elements have been traversed, we will get a sorted array.

Below is the implementation of the above approach:

C++

 // C++ implementation of the approach    #include using namespace std;    // Function to sort the array // by removing misplaced elements void removeElements(int arr[], int n) {        // brr[] is used to store     // the sorted array elements     int brr[n], l = 1;        brr = arr;     for (int i = 1; i < n; i++) {         if (brr[l - 1] <= arr[i]) {             brr[l] = arr[i];             l++;         }     }        // Print the sorted array     for (int i = 0; i < l; i++)         cout << brr[i] << " "; }    // Driver code int main() {     int arr[] = { 10, 12, 9, 10, 2, 13, 14 };     int n = sizeof(arr) / sizeof(arr);     removeElements(arr, n);        return 0; }

Java

 // Java implementation of the approach    class GFG {        // Function to sort the array // by removing misplaced elements static void removeElements(int []arr, int n) {        // brr[] is used to store     // the sorted array elements     int []brr = new int[n];     int l = 1;        brr = arr;     for (int i = 1; i < n; i++)     {         if (brr[l - 1] <= arr[i])          {             brr[l] = arr[i];             l++;         }     }        // Print the sorted array     for (int i = 0; i < l; i++)         System.out.print(brr[i] + " "); }    // Driver code static public void main (String[] args) {     int []arr = { 10, 12, 9, 10, 2, 13, 14 };     int n = arr.length;     removeElements(arr, n); } }    // This code is contributed by Code_Mech.

Python3

 # Python3 implementation of the approach     # Function to sort the array  # by removing misplaced elements  def removeElements(arr,  n) :         # brr[] is used to store      # the sorted array elements      brr = *n; l = 1;         brr = arr;      for i in range(1,n) :         if (brr[l - 1] <= arr[i]) :             brr[l] = arr[i];              l += 1;         # Print the sorted array      for i in range(l) :         print(brr[i],end=" ");        # Driver code  if __name__ == "__main__" :         arr = [ 10, 12, 9, 10, 2, 13, 14 ];      n = len(arr);      removeElements(arr, n);         # This code is contributed by AnkitRai01

C#

 // C# implementation of the approach using System;    class GFG {        // Function to sort the array // by removing misplaced elements static void removeElements(int []arr, int n) {        // brr[] is used to store     // the sorted array elements     int []brr = new int[n];     int l = 1;        brr = arr;     for (int i = 1; i < n; i++)     {         if (brr[l - 1] <= arr[i])          {             brr[l] = arr[i];             l++;         }     }        // Print the sorted array     for (int i = 0; i < l; i++)         Console.Write(brr[i] + " "); }    // Driver code static public void Main () {     int []arr = { 10, 12, 9, 10, 2, 13, 14 };     int n = arr.Length;     removeElements(arr, n); } }    // This code is contributed by jit_t

Output:

10 12 13 14

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t, Code_Mech, AnkitRai01

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.