📌  相关文章
📜  通过将成对的连续元素减为最小来检查是否所有数组元素都可以减少为0

📅  最后修改于: 2021-05-18 00:00:02             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是检查是否有可能通过从对中的两个元素中重复减去任意对连续数组元素中的最小值来将其减少为0 。如果可能,请打印“是” 。否则,打印“否”

例子:

方法:可以根据以下观察结果解决给定问题:

  • 假设arr [] = {a,b,c,d,e,f},则可以观察到a应该小于或等于b,即a≤b才能满足上述条件。否则,元素a将保持大于0
  • 现在将数组修改为arr [] = {0,b – a,c,d,e}b – a是最左边的元素,因此也适用与上述相同的条件,即b – a≤c
  • 因此可以观察到,最后重复上述过程之后,偶数索引处的元素总数必须等于奇数索引处的元素总数。

请按照以下步骤解决问题:

  • 遍历范围[1,N-1]并检查arr [i] ,然后打印“ NO”并中断。否则,将arr [i]arr [ i – 1]
  • 完成上述步骤后,如果以上情况均不满足,请检查arr [N – 1] = 0 。如果发现是真的,则打印“是” 。否则,打印“否”
  • 使用变量i遍历[0,N – 2]范围内的给定数组arr []并执行以下操作:
    • 如果arr [i]的值小于arr [i + 1],则打印“ No”,因为所有数组元素都不能减少为0
    • 否则,将arr [i + 1]arr [i]
  • 完成上述步骤后,如果以上条件均不满足且arr [N – 1]的值为0 ,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if it is possible
// to convert the array
void checkPossible(int* arr, int n)
{
    // Traverse the array range [1, N-1]
    for (int i = 1; i < n; i++) {
 
        // If arr[i] < arr[i-1]
        if (arr[i] < arr[i - 1]) {
            cout << "No\n";
            return;
        }
 
        // Otherwise
        else {
 
            // Decrement arr[i] by arr[i-1]
            arr[i] -= arr[i - 1];
            arr[i - 1] = 0;
        }
    }
 
    // If arr[n - 1] is not equal to zero
    if (arr[n - 1] == 0) {
        cout << "Yes\n";
    }
 
    // Otherwise
    else {
        cout << "No\n";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 3, 4, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    checkPossible(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
 
// Function to check if it is possible
// to convert the array
static void checkPossible(int[] arr, int n)
{
   
    // Traverse the array range [1, N-1]
    for (int i = 1; i < n; i++)
    {
 
        // If arr[i] < arr[i-1]
        if (arr[i] < arr[i - 1])
        {
            System.out.print("No\n");
            return;
        }
 
        // Otherwise
        else
        {
 
            // Decrement arr[i] by arr[i-1]
            arr[i] -= arr[i - 1];
            arr[i - 1] = 0;
        }
    }
 
    // If arr[n - 1] is not equal to zero
    if (arr[n - 1] == 0)
    {
        System.out.print("Yes\n");
    }
 
    // Otherwise
    else
    {
        System.out.print("No\n");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int arr[] = { 2, 3, 3, 4, 2 };
    int N = arr.length;
 
    // Function Call
    checkPossible(arr, N);
}
}
 
// This code is contributed by splevel62.


Python3
# Python program to implement
# the above approach
 
# Function to check if it is possible
# to convert the array
def checkPossible(arr, n):
   
    # Traverse the array range [1, N-1]
    for i in range(1, n):
 
        # If arr[i] < arr[i-1]
        if (arr[i] < arr[i - 1]):
            print("No");
            return;
 
        # Otherwise
        else:
 
            # Decrement arr[i] by arr[i-1]
            arr[i] -= arr[i - 1];
            arr[i - 1] = 0;
 
    # If arr[n - 1] is not equal to zero
    if (arr[n - 1] == 0):
        print("Yes");
 
    # Otherwise
    else:
        print("No");
 
# Driver Code
if __name__ == '__main__':
    arr = [2, 3, 3, 4, 2];
    N = len(arr);
 
    # Function Call
    checkPossible(arr, N);
 
# This code is contributed by shikhasingrajput


C#
// C# program to implement
// the above approach
using System;
class GFG
{
   
// Function to check if it is possible
// to convert the array
static void checkPossible(int[] arr, int n)
{
   
    // Traverse the array range [1, N-1]
    for (int i = 1; i < n; i++)
    {
 
        // If arr[i] < arr[i-1]
        if (arr[i] < arr[i - 1])
        {
            Console.Write("No\n");
            return;
        }
 
        // Otherwise
        else
        {
 
            // Decrement arr[i] by arr[i-1]
            arr[i] -= arr[i - 1];
            arr[i - 1] = 0;
        }
    }
 
    // If arr[n - 1] is not equal to zero
    if (arr[n - 1] == 0)
    {
        Console.Write("Yes\n");
    }
 
    // Otherwise
    else
    {
        Console.Write("No\n");
    }
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 2, 3, 3, 4, 2 };
    int N = arr.Length;
 
    // Function Call
    checkPossible(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
Yes

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