📌  相关文章
📜  通过重复减去和添加 arr[i – 1] – (i – 1) 到相邻索引,使数组严格递增

📅  最后修改于: 2021-09-04 09:38:07             🧑  作者: Mango

给定一个由N 个正整数组成的数组arr[] ,任务是检查给定的数组arr[]是否可以严格递增,使得对于范围[1, N – 1] 中的任何索引i ,如果(arr[ i – 1] – (i – 1))至少为 0 ,然后将其添加到arr[i]并从arr[i – 1] 中减去。如果可以使数组严格递增,则打印Yes 。否则,打印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[]已排序,则打印“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