给定一个由N 个正整数组成的数组arr[] ,任务是检查给定的数组arr[]是否可以严格递增,使得对于范围[1, N – 1] 中的任何索引i ,如果(arr[ i – 1] – (i – 1))至少为 0 ,然后将其添加到arr[i]并从arr[i – 1] 中减去。如果可以使数组严格递增,则打印Yes 。否则,打印No 。
例子:
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
方法:可以使用贪心方法解决给定的问题。请按照以下步骤解决问题
- 使用范围[1, N – 1] 中的变量i遍历给定数组并执行以下步骤:
- 如果arr[i – 1]至少为(i – 1) ,则执行以下步骤:
- 将arr[i] – arr[i – 1]的值存储在一个变量中,比如P 。
- 将arr[i – 1]更新为arr[i – 1] – P 。
- 将arr[i]更新为arr[i] + P 。
- 如果arr[i – 1]至少为(i – 1) ,则执行以下步骤:
- 完成上述步骤后,如果数组arr[]已排序,则打印“Yes” 。否则,打印“否”
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if an array
// can be made strictly increasing
void check(int arr[], int n)
{
// Traverse the given array arr[]
for (int i = 1; i < n; i++) {
if (arr[i - 1] >= (i - 1)) {
// Update the value of p,
// arr[i], and arr[i - 1]
int p = arr[i - 1] - (i - 1);
arr[i] += p;
arr[i - 1] -= p;
}
}
// Traverse the given array
for (int i = 1; i < n; i++) {
// Check if the array arr[] is
// strictly increasing or not
if (arr[i] <= arr[i - 1]) {
cout << "No";
return;
}
}
// Otherwise, array is increasing
// order, print Yes
cout << "Yes";
}
// Driver Code
int main()
{
int arr[] = { 1, 5, 2, 7, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
check(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to check if an array
// can be made strictly increasing
static void check(int arr[], int n)
{
// Traverse the given array arr[]
for(int i = 1; i < n; i++)
{
if (arr[i - 1] >= (i - 1))
{
// Update the value of p,
// arr[i], and arr[i - 1]
int p = arr[i - 1] - (i - 1);
arr[i] += p;
arr[i - 1] -= p;
}
}
// Traverse the given array
for(int i = 1; i < n; i++)
{
// Check if the array arr[] is
// strictly increasing or not
if (arr[i] <= arr[i - 1])
{
System.out.println("No");
return;
}
}
// Otherwise, array is increasing
// order, print Yes
System.out.println("Yes");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 5, 2, 7, 6 };
int N = arr.length;
check(arr, N);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
# Function to check if an array
# can be made strictly increasing
def check(arr, n):
# Traverse the given array arr[]
for i in range(1, n):
if (arr[i - 1] >= (i - 1)):
# Update the value of p,
# arr[i], and arr[i - 1]
p = arr[i - 1] - (i - 1)
arr[i] += p
arr[i - 1] -= p
# Traverse the given array
for i in range(1, n):
# Check if the array arr[] is
# strictly increasing or not
if (arr[i] <= arr[i - 1]):
print ("No")
return
# Otherwise, array is increasing
# order, prYes
print ("Yes")
# Driver Code
if __name__ == '__main__':
arr = [1, 5, 2, 7, 6]
N = len(arr)
check(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if an array
// can be made strictly increasing
static void check(int []arr, int n)
{
// Traverse the given array arr[]
for(int i = 1; i < n; i++)
{
if (arr[i - 1] >= (i - 1))
{
// Update the value of p,
// arr[i], and arr[i - 1]
int p = arr[i - 1] - (i - 1);
arr[i] += p;
arr[i - 1] -= p;
}
}
// Traverse the given array
for(int i = 1; i < n; i++)
{
// Check if the array arr[] is
// strictly increasing or not
if (arr[i] <= arr[i - 1])
{
Console.Write("No");
return;
}
}
// Otherwise, array is increasing
// order, print Yes
Console.Write("Yes");
}
// Driver Code
public static void Main()
{
int []arr = { 1, 5, 2, 7, 6 };
int N = arr.Length;
check(arr, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live