Make the array non-decreasing with the given operation
Last Updated :
07 Mar, 2022
Given an array arr[] of size N, the task is to check if it is possible to make the array non-decreasing by applying the given operation at most once on each array element. In a single operation, one can decrease the element by one i.e. arr[i] = arr[i] – 1.
Examples:
Input: arr[] = {1, 2, 1, 2, 3}
Output: Yes
Apply the given operation on the 2nd and the 4th element.
Now, the array becomes {1, 1, 1, 1, 3}
Input: arr[] = {1, 3, 1}
Output: No
Approach: Process the elements in increasing order and decrease the current element whenever it can be done without making it less than the previous element. (The first element should thus always be decreased.) If at any point the current element is less than the previous element then no matter what operation is performed, the answer is “No”.
The reason this strategy is optimal is that decreasing a number will make it easier (or at least easy) to deal with the next element in the list, since any value the next element could have taken will still work when the previous number is lower, and in fact decreasing the previous number expands the range of possible values for the next set.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
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" );
}
}
|
Python3
def isPossible(a, n) :
cur = a[ 0 ];
cur - = 1 ;
for i in range ( 1 , n) :
nxt = a[i];
if (nxt > cur) :
nxt - = 1 ;
elif (nxt < cur) :
return False ;
cur = nxt;
return True ;
if __name__ = = "__main__" :
a = [ 1 , 2 , 1 , 2 , 3 ];
n = len (a);
if (isPossible(a, n)) :
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
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))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isPossible(a, n)
{
var cur = a[0];
cur--;
for ( var i = 1; i < n; i++)
{
var nxt = a[i];
if (nxt > cur)
nxt--;
else if (nxt < cur)
return false ;
cur = nxt;
}
return true ;
}
var a = [ 1, 2, 1, 2, 3 ];
var n = a.length;
if (isPossible(a, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...