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 possible to make the array strictly increasing, then print Yes. Otherwise, print No.
Examples:
Input: arr[] = {1, 5, 2, 7, 6}
Output: Yes
Explanation:
Consider the following operations:
- Choosing the index 1, the value of arr[i – 1] – (i – 1) is 1, which is at least 0. Adding 1 to arr[1] and subtracting it from arr[0], modifies the array to {0, 6, 2, 7, 6}.
- Choosing the index 2, the value of arr[i – 1] – (i – 1) is 5, which is at least 0. Adding 5 to arr[2] and subtracting it from arr[1], modifies the array to {0, 1, 7, 7, 6}.
- Choosing the index 3, the value of arr[i – 1] – (i – 1) is 5, which is at least 0. Adding 6 to arr[3] and subtracting it from arr[2], modifies the array to {0, 1, 2, 12, 6}.
- Choosing the index 4, the value of arr[i – 1] – (i – 1) is 9, which is at least 0. Adding 9 to arr[4] and subtracted from arr[3], modifies the array to {0, 1, 2, 3, 15}.
After the above operations, the array becomes strictly increasing.
Input: arr[] = {0, 1, 0}
Output: No
Approach: The given problem can be solved by using the Greedy Approach. Follow the steps below to solve the problem
- Traverse the given array using variable i in range [1, N – 1] and perform the following steps:
- If arr[i – 1] is at least (i – 1), then perform the following steps:
- Store the value of arr[i] – arr[i – 1] in a variable, say P.
- Update arr[i – 1] as arr[i – 1] – P.
- Update arr[i] as arr[i] + P.
- After completing the above steps, if the array arr[] is sorted, then print “Yes”. Otherwise, print “No”
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void check( int arr[], int n)
{
for ( int i = 1; i < n; i++) {
if (arr[i - 1] >= (i - 1)) {
int p = arr[i - 1] - (i - 1);
arr[i] += p;
arr[i - 1] -= p;
}
}
for ( int i = 1; i < n; i++) {
if (arr[i] <= arr[i - 1]) {
cout << "No" ;
return ;
}
}
cout << "Yes" ;
}
int main()
{
int arr[] = { 1, 5, 2, 7, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
check(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void check( int arr[], int n)
{
for ( int i = 1 ; i < n; i++)
{
if (arr[i - 1 ] >= (i - 1 ))
{
int p = arr[i - 1 ] - (i - 1 );
arr[i] += p;
arr[i - 1 ] -= p;
}
}
for ( int i = 1 ; i < n; i++)
{
if (arr[i] <= arr[i - 1 ])
{
System.out.println( "No" );
return ;
}
}
System.out.println( "Yes" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 2 , 7 , 6 };
int N = arr.length;
check(arr, N);
}
}
|
Python3
def check(arr, n):
for i in range ( 1 , n):
if (arr[i - 1 ] > = (i - 1 )):
p = arr[i - 1 ] - (i - 1 )
arr[i] + = p
arr[i - 1 ] - = p
for i in range ( 1 , n):
if (arr[i] < = arr[i - 1 ]):
print ( "No" )
return
print ( "Yes" )
if __name__ = = '__main__' :
arr = [ 1 , 5 , 2 , 7 , 6 ]
N = len (arr)
check(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void check( int []arr, int n)
{
for ( int i = 1; i < n; i++)
{
if (arr[i - 1] >= (i - 1))
{
int p = arr[i - 1] - (i - 1);
arr[i] += p;
arr[i - 1] -= p;
}
}
for ( int i = 1; i < n; i++)
{
if (arr[i] <= arr[i - 1])
{
Console.Write( "No" );
return ;
}
}
Console.Write( "Yes" );
}
public static void Main()
{
int []arr = { 1, 5, 2, 7, 6 };
int N = arr.Length;
check(arr, N);
}
}
|
Javascript
<script>
function check(arr, n)
{
for ( var i = 1; i < n; i++)
{
if (arr[i - 1] >= (i - 1))
{
var p = arr[i - 1] - (i - 1);
arr[i] += p;
arr[i - 1] -= p;
}
}
for ( var i = 1; i < n; i++)
{
if (arr[i] <= arr[i - 1])
{
document.write( "No" );
return ;
}
}
document.write( "Yes" );
}
var arr = [ 1, 5, 2, 7, 6 ];
var N = arr.length;
check(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
06 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...