Reduce the array by deleting elements which are greater than all elements to its left | C ++ program for the above approach ; Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Driver Code ; Given array arr [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > merge ( vector < int > x , vector < int > y ) { for ( auto i : y ) { if ( x [ x . size ( ) - 1 ] > i ) x . push_back ( i ) ; } return x ; } vector < int > mergeDel ( vector < int > l ) { if ( l . size ( ) == 1 ) return l ; int m = l . size ( ) / 2 ; vector < int > temp1 = { l . begin ( ) + 0 , l . begin ( ) + m } ; vector < int > temp2 = { l . begin ( ) + m , l . end ( ) } ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } int main ( ) { vector < int > arr ( { 5 , 4 , 3 , 2 , 1 } ) ; vector < int > ans = mergeDel ( arr ) ; cout << " [ ▁ " ; for ( auto x : ans ) cout << x << " , ▁ " ; cout << " ] " ; }