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 ; Driver code ; Input ; Function call
def minimumDeletions ( A , N ) : NEW_LINE INDENT ans = 10 ** 8 NEW_LINE for i in range ( 1 , ( 1 << N ) ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( ( i & ( 1 << j ) ) != 0 ) : NEW_LINE INDENT temp . append ( A [ j ] ) NEW_LINE DEDENT DEDENT flag = 0 NEW_LINE for j in range ( 1 , len ( temp ) ) : NEW_LINE INDENT if ( temp [ j ] < temp [ j - 1 ] ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT for j in range ( 1 , len ( temp ) - 1 ) : NEW_LINE INDENT if ( temp [ j ] - temp [ j - 1 ] > temp [ j + 1 ] - temp [ j ] ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT ans = min ( ans , N - len ( temp ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 5 , 7 , 20 , 21 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumDeletions ( A , N ) ) NEW_LINE DEDENT