If the i - th index has any any point in common with the ( i - 1 ) th index classify it as '1' in resultant array and update maxR if necessary else we have found the breakpoint and we can exit the loop
bool ok = false ; for ( int i = 1 ; i < n ; i ++ ) { if ( maxR >= vec [ i ] . first ) { res [ vec [ i ] . second . second ] = res [ vec [ i - 1 ] . second . second ] ; maxR = max ( maxR , vec [ i ] . second . first ) ; } else { ok = true ; break ; } } if ( ok ) { for ( auto x : res ) cout << x << " ▁ " ; cout << ' ' ; } else cout << " Not ▁ possible STRNEWLINE " ; }