Check if an Array can be Sorted by picking only the corner Array elements | C ++ Program to implement the above approach ; Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int n ) { int i , g ; g = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 0 && g == 1 ) return false ; if ( arr [ i ] - arr [ i - 1 ] < 0 ) g = 1 ; } return true ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 10 , 4 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; if ( check ( arr , n ) == true ) cout << " Yes " " STRNEWLINE " ; else cout << " No " << " STRNEWLINE " ; return 0 ; }