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
function isPossibleToSort ( arr , N ) { var idx = - 1 ; var i ; for ( i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == - 1 ) { document . write ( " " ) ; return ; } for ( i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var arr = [ 1 , 0 , 1 , 0 , 1 , 1 , 0 ] ; var N = arr . length ; isPossibleToSort ( arr , N ) ;