Make the array non | C ++ implementation of the approach ; Function to make array non - decreasing ; Take the first element ; Perform the operation ; Traverse the array ; Next element ; If next element is greater than the current element then decrease it to increase the possibilities ; It is not possible to make the array non - decreasing with the given operation ; Next element is now the current ; The array can be made non - decreasing with the given operation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int a [ ] , int n ) { int cur = a [ 0 ] ; cur -- ; for ( int i = 1 ; i < n ; i ++ ) { int nxt = a [ i ] ; if ( nxt > cur ) nxt -- ; else if ( nxt < cur ) return false ; cur = nxt ; } return true ; } int main ( ) { int a [ ] = { 1 , 2 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isPossible ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }