Check whether an array can be made strictly increasing by incrementing and decrementing adjacent pairs
Last Updated :
16 Dec, 2021
Given an array arr[] of size N consisting of non-negative integers. In one move ith index element of the array is decreased by 1 and (i+1)th index is increased by 1. The task is to check if there is any possibility to make the given array strictly increasing (containing non-negative integers only) by making any number of moves.
Examples:
Input: arr[3] = [1, 2, 3]
Output: Yes
Explanation: The array is already sorted in strictly increasing order.
Input: arr[2] = [2, 0]
Output: Yes
Explanation: Consider i = 0 for the 1st move arr[0] = 2-1 = 1, arr[1] = 0 + 1 = 1. Now the array becomes, [1, 1].
In 2nd move consider i = 0. So now arr[0] = 1 - 1 = 0, arr[1] = 1 + 1 = 2.
The final array becomes, arr[2] = [0, 2] which is strictly increasing.
Input: arr[3] = [0, 1, 0]
Output: No
Explanation: This array cannot be made strictly increasing containing only non negative integers by performing any number of moves.
Approach: The problem can be solved using the following mathematical observation.
- Since all the array elements are non-negative, so minimum strictly increasing order of an array of size N can be: 0, 1, 2, 3 . . . (N-1).
- So the minimum sum(min_sum) of first i elements (till (i-t)th index) of any such array is min_sum = (i*(i-1))/2.
- Therefore, the sum of first i elements of given array(cur_sum) must satisfy the condition cur_sum ≥ min_sum .
- If the condition is not satisfied, then it is not possible to make the given array strictly increasing. Consider the following example
Illustration 1:
arr[] = 4 5 1 2 3
min_sum = 0 1 3 6 10
sum(arr) = 4 9 10 12 15
As this array satisfies the condition for every i, it is possible to convert this array to strictly increasing array
Illustration 2:
arr[] = 2 3 1 0 2
min_sum = 0 1 3 6 10
sum(arr) = 2 5 6 6 8
Here at index 4 the sum of array does not satisfy the condition of having minimum sum 10. So it is not possible to change the array into a strictly increasing one.
Follow the steps mentioned below to implement the concept:
- Traverse from index = 0 to index = N - 1, and for each i check if sum till that is greater than or equal to (i*(i+1))/2.
- If the condition is satisfied then the array can be made strictly increasing. Otherwise, it cannot be made strictly increasing.
Follow the below implementation for the above approach:
C++
// C++ code to check if the given array
// can be made strictly increasing
#include <bits/stdc++.h>
using namespace std;
// Function to check if
// the array can be made strictly increasing
void CheckStrictlyIncreasing(int arr[],
int N)
{
// variable to store sum till current
// index element
int cur_sum = 0;
bool possible = true;
for (int index = 0;
index < N; index++) {
cur_sum += arr[index];
// Sum of 0, 1, ...(i)th element
int req_sum = (index * (index + 1)) / 2;
// Check if valid or not
if (req_sum > cur_sum) {
possible = false;
break;
}
}
// If can be made strictly increasing
if (possible)
cout << "Yes";
else
cout << "No";
}
// Driver code
int main()
{
int arr[3] = { 1, 2, 3 };
int N = 3;
CheckStrictlyIncreasing(arr, N);
return 0;
}
Java
// Java code to check if the given array
// can be made strictly increasing
import java.util.*;
class GFG{
// Function to check if
// the array can be made strictly increasing
static void CheckStrictlyIncreasing(int arr[],
int N)
{
// variable to store sum till current
// index element
int cur_sum = 0;
boolean possible = true;
for (int index = 0;
index < N; index++) {
cur_sum += arr[index];
// Sum of 0, 1, ...(i)th element
int req_sum = (index * (index + 1)) / 2;
// Check if valid or not
if (req_sum > cur_sum) {
possible = false;
break;
}
}
// If can be made strictly increasing
if (possible)
System.out.print("Yes");
else
System.out.print("No");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3 };
int N = 3;
CheckStrictlyIncreasing(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 code to check if the given array
# can be made strictly increasing
# Function to check if
# the array can be made strictly increasing
def CheckStrictlyIncreasing(arr,
N):
# variable to store sum till current
# index element
cur_sum = 0
possible = True
for index in range(N):
cur_sum += arr[index]
# Sum of 0, 1, ...(i)th element
req_sum = (index * (index + 1)) // 2
# Check if valid or not
if (req_sum > cur_sum):
possible = False
break
# If can be made strictly increasing
if (possible):
print("Yes")
else:
print("No")
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3]
N = 3
CheckStrictlyIncreasing(arr, N)
# This code is contributed by ukasp.
C#
// C# code to check if the given array
// can be made strictly increasing
using System;
class GFG{
// Function to check if the array can
// be made strictly increasing
static void CheckStrictlyIncreasing(int []arr,
int N)
{
// Variable to store sum till current
// index element
int cur_sum = 0;
bool possible = true;
for(int index = 0;
index < N; index++)
{
cur_sum += arr[index];
// Sum of 0, 1, ...(i)th element
int req_sum = (index * (index + 1)) / 2;
// Check if valid or not
if (req_sum > cur_sum)
{
possible = false;
break;
}
}
// If can be made strictly increasing
if (possible)
Console.Write("Yes");
else
Console.Write("No");
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3 };
int N = 3;
CheckStrictlyIncreasing(arr, N);
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript code for the above approach
// Function to check if
// the array can be made strictly increasing
function CheckStrictlyIncreasing(arr, N)
{
// variable to store sum till current
// index element
let cur_sum = 0;
let possible = true;
for (let index = 0;
index < N; index++) {
cur_sum += arr[index];
// Sum of 0, 1, ...(i)th element
let req_sum = (index * (index + 1)) / 2;
// Check if valid or not
if (req_sum > cur_sum) {
possible = false;
break;
}
}
// If can be made strictly increasing
if (possible)
document.write("Yes");
else
document.write("No");
}
// Driver code
let arr = [1, 2, 3];
let N = 3;
CheckStrictlyIncreasing(arr, N);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Space Complexity: O(1)
Similar Reads
Check if Array can be made strictly increasing by replacing element with Average of adjacents Given an array A[] of size N, the task is to check whether the sequence can be made strictly increasing by performing the following operation any number of times(0 or more): Select any two adjacent elements Ai and Ai+1 and replace one of them with (Ai + Ai+1) / 2 (a real number, i.e. without roundin
4 min read
Check whether an array can be made strictly increasing by modifying atmost one element Given an array arr[] of positive integers, the task is to find whether it is possible to make this array strictly increasing by modifying atmost one element.Examples: Input: arr[] = {2, 4, 8, 6, 9, 12} Output: Yes By modifying 8 to 5, array will become strictly increasing. i.e. {2, 4, 5, 6, 9, 12}In
8 min read
Check whether an array can be made strictly increasing by removing at most one element Given an array arr[] consisting of N integers, the task is to check whether it is possible to make the given array strictly increasing by removing at most one element. If it is possible to make the array strictly increasing, then print "Yes". Otherwise, print "No". Examples: Input: arr[] = {1, 1, 2}
8 min read
Check whether an array can be made strictly decreasing by modifying at most one element Given an array arr[] of positive integers, the task is to find whether it is possible to make this array strictly decreasing by modifying at most one element.Examples: Input: arr[] = {12, 9, 10, 5, 2} Output: Yes {12, 11, 10, 5, 2} is one of the valid solutions.Input: arr[] = {1, 2, 3, 4} Output: No
8 min read
Check if Array can be made strictly increasing by merging three consecutive elements M times Given an array arr[] of length N and an integer M, the task is to determine if a strictly increasing sequence can be formed by merging three consecutive elements exactly M times. Note: Merging three elements means removing all three of them and inserting a single element having value same as the sum
11 min read
Sort an Array by decreasing and increasing elements with adjacent swaps Given an array arr[] of size N. The task is to make an array in increasing order where you are allowed (any number of times) to swap adjacent elements and decrease the value with a smaller index by 1 and increase the value of a greater index by 1. Output "Yes" if it is possible to arrange the array
7 min read
Split the array elements into strictly increasing and decreasing sequence Given an array of N elements. The task is to split the elements into two arrays say a1[] and a2[] such that one contains strictly increasing elements and the other contains strictly decreasing elements and a1.size() + a2.size() = a.size(). If it is not possible to do so, print -1 or else print both
7 min read
Check whether Array can be made strictly increasing by shifting 1 value to the right Given an array arr[] of N positive integers, the task is to check whether an array can be made strictly increasing by shifting 1 value to its right element any number of times (i.e, for any value of i, decrement arr[i] and increment arr[i + 1] by 1). Note: The integer at any index after any operatio
5 min read
Make an array strictly increasing by repeatedly subtracting and adding arr[i - 1] - (i - 1) to adjacent indices Given an array arr[] consisting of N positive integers, the task is to check whether the given array arr[] can be made strictly increasing such that for any index i from the range [1, N - 1], if (arr[i - 1] - (i - 1)) is at least 0, then it is added to arr[i] and subtracted from arr[i - 1]. If it is
6 min read
Check if Array element can be incremented to X by using given operations Given an array A[] of size N, and an integer X, the following operation can be performed on that array: Choose any element, say Y from the array.Then every element in the array except Y are incremented.In the next step, only the value Y+1 can be chosen and the steps are repeated until Y+1 is not pre
7 min read