📌  相关文章
📜  检查 Array 是否可以通过右移 1 来严格递增

📅  最后修改于: 2022-05-13 01:56:05.840000             🧑  作者: Mango

检查 Array 是否可以通过右移 1 来严格递增

给定一个由N个正整数组成的数组arr[] ,任务是检查一个数组是否可以通过将1值移至其右元素任意次数来严格增加(即,对于i的任何值,递减arr[i]并将arr[i + 1]增加1 )。

注意:任何操作后任何索引处的整数都不能为负数。

例子:

方法:给定的问题可以使用贪心方法来解决。这个想法是,对于任何索引i ,不包括负整数的最小可能严格递增序列是0, 1, 2, 3, ...。因此,对于[0, N)范围内的i的每个值,直到arr[i]的整数之和必须大于系列0, 1, 2, ..., i-1的和,这等价于(我 * (i – 1))/2 。因此,如果给定的数组满足该条件,则返回“”,否则,返回“”。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to check whether the given
// array can be made strictly increasing
// by shifting 1 value to the right
bool isPossible(int arr[], int N)
{
    // Stores the sum of arr[]
    int sum = 0;
 
    // Loop to traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update sum
        sum += arr[i];
 
        // If sum is less than the
        // least required value
        if (sum < i * (i + 1) / 2)
            return false;
    }
 
    // Return possible
    return true;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 5, 4 };
    int N = sizeof(arr) / sizeof(int);
 
    if (isPossible(arr, N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
 
  // Function to check whether the given
  // array can be made strictly increasing
  // by shifting 1 value to the right
  static Boolean isPossible(int arr[], int N)
  {
     
    // Stores the sum of arr[]
    int sum = 0;
 
    // Loop to traverse the array
    for (int i = 0; i < N; i++) {
 
      // Update sum
      sum += arr[i];
 
      // If sum is less than the
      // least required value
      if (sum < i * (i + 1) / 2)
        return false;
    }
 
    // Return possible
    return true;
  }
 
  public static void main (String[] args) {
    int arr[] = { 4, 5, 4 };
    int N = arr.length;
 
    if (isPossible(arr, N)) {
      System.out.print("Yes");
    }
    else {
      System.out.print("No");
    }
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# Python code for the above approach
 
# Function to check whether the given
# array can be made strictly increasing
# by shifting 1 value to the right
def isPossible(arr, N):
 
    # Stores the sum of arr[]
    sum = 0
 
    # Loop to traverse the array
    for i in range(N):
 
        # Update sum
        sum += arr[i]
 
        # If sum is less than the
        # least required value
        if sum < i * (i + 1) / 2:
            return 0
 
    # Return possible
    return 1
 
# Driver Code
arr = [4, 5, 4]
N = len(arr)
 
if isPossible(arr, N) == 1:
    print("Yes")
else:
    print("No")
     
# This code is contributed by Potta Lokesh


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to check whether the given
  // array can be made strictly increasing
  // by shifting 1 value to the right
  static Boolean isPossible(int []arr, int N)
  {
 
    // Stores the sum of arr[]
    int sum = 0;
 
    // Loop to traverse the array
    for (int i = 0; i < N; i++) {
 
      // Update sum
      sum += arr[i];
 
      // If sum is less than the
      // least required value
      if (sum < i * (i + 1) / 2)
        return false;
    }
 
    // Return possible
    return true;
  }
 
  public static void Main () {
    int []arr = { 4, 5, 4 };
    int N = arr.Length;
 
    if (isPossible(arr, N)) {
      Console.Write("Yes");
    }
    else {
      Console.Write("No");
    }
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
Yes

时间复杂度: O(N)
辅助空间: O(1)