Remove elements to make array sorted | C ++ implementation of the approach ; Function to sort the array by removing misplaced elements ; l stores the index ; Print the sorted array ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }