Minimum deletions in Array to make difference of adjacent elements non | Java program for the above approach ; Function for finding minimum deletions so that the array becomes non decreasing and the difference between adjacent elements also becomes non decreasing ; initialize answer to a large value ; generating all subsets ; checking the first condition ; checking the second condition ; if both conditions are satisfied consider the answer for minimum ; Driver code ; Input ; Function call
import java . util . ArrayList ; class GFG { public static int minimumDeletions ( int A [ ] , int N ) { int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i < ( 1 << N ) ; i ++ ) { ArrayList < Integer > temp = new ArrayList < Integer > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { temp . add ( A [ j ] ) ; } } int flag = 0 ; for ( int j = 1 ; j < temp . size ( ) ; j ++ ) if ( temp . get ( j ) < temp . get ( j - 1 ) ) flag = 1 ; for ( int j = 1 ; j < temp . size ( ) - 1 ; j ++ ) if ( temp . get ( j ) - temp . get ( j - 1 ) > temp . get ( j + 1 ) - temp . get ( j ) ) flag = 1 ; if ( flag == 0 ) { ans = Math . min ( ans , N - ( int ) temp . size ( ) ) ; } } return ans ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . length ; System . out . println ( minimumDeletions ( A , N ) ) ; } }