检查是否可以通过递增和递减相邻对来使数组严格递增
给定一个由非负整数组成的大小为N的数组arr[] 。在一次移动中,数组的第 i 个索引元素减少 1,第 (i+1) 个索引增加 1。任务是检查是否有可能使给定数组严格增加(仅包含非负整数) 通过进行任意数量的移动。
例子:
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.
方法:可以使用以下数学观察来解决问题。
- 由于所有数组元素都是非负数,因此大小为N的数组的最小严格递增顺序可以是: 0, 1, 2, 3 。 . . (N-1) 。
- 因此,任何此类数组的前 i个元素(直到第 (it) 个索引)的最小 sum(min_sum)为min_sum = (i*(i-1))/2。
- 因此,给定数组(cur_sum)的前 i 个元素的总和必须满足条件cur_sum ≥ min_sum 。
- 如果条件不满足,则不可能使给定数组严格递增。考虑以下示例
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.
按照下面提到的步骤来实现这个概念:
- 从index = 0遍历到index = N – 1 ,并为每个 i检查总和是否大于或等于(i*(i+1))/2 。
- 如果满足条件,则可以使数组严格递增。否则不能严格递增。
对于上述方法,请遵循以下实现:
C++
// C++ code to check if the given array
// can be made strictly increasing
#include
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
Yes
时间复杂度: O(N)
空间复杂度: O(1)