Check if removal of a subsequence of non | Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver Code
def isPossibleToSort ( arr , N ) : NEW_LINE INDENT idx = - 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] == 1 and arr [ i - 1 ] == 1 ) : NEW_LINE INDENT idx = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( idx == - 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE return NEW_LINE DEDENT for i in range ( idx + 1 , N , 1 ) : NEW_LINE INDENT if ( arr [ i ] == 0 and arr [ i - 1 ] == 0 ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " YES " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 , 1 , 0 ] NEW_LINE N = len ( arr ) NEW_LINE isPossibleToSort ( arr , N ) NEW_LINE DEDENT