Minimum deletions in Array to make difference of adjacent elements non | 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 ; Input ; Function call
function minimumDeletions ( A , N ) { let ans = Number . MAX_SAFE_INTEGER ; for ( let i = 1 ; i < ( 1 << N ) ; i ++ ) { let temp = [ ] ; for ( let j = 0 ; j < N ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { temp . push ( A [ j ] ) ; } } let flag = 0 ; for ( let j = 1 ; j < temp . length ; j ++ ) if ( temp [ j ] < temp [ j - 1 ] ) flag = 1 ; for ( let j = 1 ; j < temp . length - 1 ; j ++ ) if ( temp [ j ] - temp [ j - 1 ] > temp [ j + 1 ] - temp [ j ] ) flag = 1 ; if ( flag == 0 ) { ans = Math . min ( ans , N - temp . length ) ; } } return ans ; } let A = [ 1 , 4 , 5 , 7 , 20 , 21 ] ; let N = A . length ; document . write ( minimumDeletions ( A , N ) + " " ) ;