Reduce the array by deleting elements which are greater than all elements to its left | Java 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 [ ]
import java . util . ArrayList ; import java . util . Arrays ; class GFG { static ArrayList < Integer > merge ( ArrayList < Integer > x , ArrayList < Integer > y ) { for ( Integer i : y ) { if ( x . get ( x . size ( ) - 1 ) > i ) x . add ( i ) ; } return x ; } static ArrayList < Integer > mergeDel ( ArrayList < Integer > l ) { if ( l . size ( ) == 1 ) return l ; int m = l . size ( ) / 2 ; ArrayList < Integer > temp1 = new ArrayList < > ( l . subList ( 0 , m ) ) ; ArrayList < Integer > temp2 = new ArrayList < > ( l . subList ( m , l . size ( ) ) ) ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } public static void main ( String [ ] args ) { Integer [ ] ar = { 5 , 4 , 3 , 2 , 1 } ; ArrayList < Integer > arr = new ArrayList < > ( Arrays . asList ( ar ) ) ; ArrayList < Integer > ans = mergeDel ( arr ) ; System . out . print ( " [ ▁ " ) ; for ( Integer x : ans ) System . out . print ( x + " , ▁ " ) ; System . out . println ( " ] " ) ; } }