Check if an Array can be Sorted by picking only the corner Array elements | 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
def check ( arr , n ) : NEW_LINE INDENT g = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] > 0 and g == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( arr [ i ] - arr [ i ] < 0 ) : NEW_LINE INDENT g = 1 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 10 , 4 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT