Check if removal of a subsequence of non | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isPossibleToSort ( int arr [ ] , int N ) { int idx = -1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == -1 ) { cout << " YES " ; return ; } for ( int i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { cout << " NO " ; return ; } } cout << " YES " ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isPossibleToSort ( arr , N ) ; return 0 ; }