Make the array non | Java 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
class GFG { static boolean 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 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 3 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }