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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to make array non-decreasing
bool isPossible(int a[], int n)
{
// Take the first element
int cur = a[0];
// Perform the operation
cur--;
// Traverse the array
for (int i = 1; i < n; i++) {
// Next element
int nxt = a[i];
// If next element is greater than the
// current element then decrease
// it to increase the possibilities
if (nxt > cur)
nxt--;
// It is not possible to make the
// array non-decreasing with
// the given operation
else if (nxt < cur)
return false;
// Next element is now the current
cur = nxt;
}
// The array can be made non-decreasing
// with the given operation
return true;
}
// Driver code
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
// Java implementation of the approach
class GFG
{
// Function to make array non-decreasing
static boolean isPossible(int a[], int n)
{
// Take the first element
int cur = a[0];
// Perform the operation
cur--;
// Traverse the array
for (int i = 1; i < n; i++)
{
// Next element
int nxt = a[i];
// If next element is greater than the
// current element then decrease
// it to increase the possibilities
if (nxt > cur)
nxt--;
// It is not possible to make the
// array non-decreasing with
// the given operation
else if (nxt < cur)
return false;
// Next element is now the current
cur = nxt;
}
// The array can be made non-decreasing
// with the given operation
return true;
}
// Driver code
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");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to make array non-decreasing
def isPossible(a, n) :
# Take the first element
cur = a[0];
# Perform the operation
cur -= 1;
# Traverse the array
for i in range(1, n) :
# Next element
nxt = a[i];
# If next element is greater than the
# current element then decrease
# it to increase the possibilities
if (nxt > cur) :
nxt -= 1;
# It is not possible to make the
# array non-decreasing with
# the given operation
elif (nxt < cur) :
return False;
# Next element is now the current
cur = nxt;
# The array can be made non-decreasing
# with the given operation
return True;
# Driver code
if __name__ == "__main__" :
a = [ 1, 2, 1, 2, 3 ];
n = len(a);
if (isPossible(a, n)) :
print("Yes");
else :
print("No");
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to make array non-decreasing
static Boolean isPossible(int []a, int n)
{
// Take the first element
int cur = a[0];
// Perform the operation
cur--;
// Traverse the array
for (int i = 1; i < n; i++)
{
// Next element
int nxt = a[i];
// If next element is greater than the
// current element then decrease
// it to increase the possibilities
if (nxt > cur)
nxt--;
// It is not possible to make the
// array non-decreasing with
// the given operation
else if (nxt < cur)
return false;
// Next element is now the current
cur = nxt;
}
// The array can be made non-decreasing
// with the given operation
return true;
}
// Driver code
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");
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript implementation of the approach
// Function to make array non-decreasing
function isPossible(a, n)
{
// Take the first element
var cur = a[0];
// Perform the operation
cur--;
// Traverse the array
for(var i = 1; i < n; i++)
{
// Next element
var nxt = a[i];
// If next element is greater than the
// current element then decrease
// it to increase the possibilities
if (nxt > cur)
nxt--;
// It is not possible to make the
// array non-decreasing with
// the given operation
else if (nxt < cur)
return false;
// Next element is now the current
cur = nxt;
}
// The array can be made non-decreasing
// with the given operation
return true;
}
// Driver Code
var a = [ 1, 2, 1, 2, 3 ];
var n = a.length;
if (isPossible(a, n))
document.write("Yes");
else
document.write("No");
// This code is contributed by Khushboogoyal499
</script>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Operations to Sort an Array in non-decreasing order Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.arr[i] ⤠arr[i+1]) by using two types of operation by performing any numbers of time: You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1]
7 min read
Minimize increment operations to make Array non-decreasing Given an array arr[] of n integers. Modify the array such that every element is at least as large as the previous element. This can be done by increasing the value of any element by 1. The task is to find the minimum number of moves required to make the array non-decreasing. Examples: Input: n = 5,
5 min read
Minimum increment operations to make the array in increasing order Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array. Examples: Input : a = { 1, 3, 3, 2 }, X = 2 Output : 3 Explanation : Modified array is { 1, 3, 5, 6 } Input : a = { 3, 5, 6 }, X = 5 Output : 0 O
6 min read
Minimize Operation to Make Array Non-decreasing or Non-increasing Given an integer array arr[]. In one operation, you can choose an index i and either increment or decrement arr[i] by 1. The task is to find the minimum number of operations needed to make arr either non-decreasing or non-increasing. Example: Input: arr = [3, 2, 4, 5, 0]Output: 4Explanation: One pos
8 min read
Minimum increments to make the array non-decreasing Given an array A[] of size N along with an integer M such that 0 <= A[i] < M, the task is to output minimum number of operations required to sort A[] to non-decreasing order using following operation, choose an element let say A[i] and update it as (A[i] + 1) mod M. Examples: Input: N = 4, M =
7 min read