📌  相关文章
📜  通过将数组元素的索引值重复减少来检查数组的和是否可以减少到零

📅  最后修改于: 2021-05-14 08:34:51             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是确定是否可以通过多次执行以下操作来确定数组元素的总和是否可以减少为0

  • 选择的元素A [i]和减少A [I]i(基于1的索引),任意次数,可能0。
  • 如果总和可以减少到0 ,则打印“”。否则,打印“ No ”。

例子:

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

  • 如果一个正数反复从另一个正数中减去,然后最终,0可以得到。但是,从负数中重复减去一个正数,将永远无法获得0,因为它一直在不断减少。
  • 通过从A [i]中减去i将总和减少为0的任务。
  • 因此,在选择元素并将元素的值减少i时总和会减少i
  • 因此,任务简化为检查数组的初始和为正还是0 。如果发现是真的,则打印“”。否则,打印“ No ”。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to check if an array
// sum can be reduced to zero or not
bool isPossible(int arr[], int n)
{
    // Stores the sum of the array
    int S = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Update sum of the array
        S = S + arr[i];
    }
 
    // If the sum is positive
    if (S >= 0) {
 
        // Array sum can be
        // reduced to 0
        return true;
    }
 
    // Otherwise
    else {
 
        // Array sum cannot
        // be reduced to 0
        return false;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { -5, 3 };
 
    int n = sizeof(arr) / sizeof(int);
 
    // Print the answer
    if (isPossible(arr, n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}


Java
// Java program for teh above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if array sum
    // can be reduced to zero or not
    static boolean isPossible(int[] arr, int n)
    {
        // Stores the sum of the array
        int S = 0;
 
        for (int i = 0; i < n; i++) {
            S = S + arr[i];
        }
 
        // If array sum is positive
        if (S >= 0)
 
            // Array sum can be
            // reduced to 0
            return true;
 
        // Otherwise
        else
 
            // Array sum cannot
            // be reduced to 0
            return false;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { -5, 3 };
        int n = arr.length;
 
        // Function call
        if (isPossible(arr, n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3
# Python program for the above approach
 
# Function to check if an array
# sum can be reduced to zero or not
def isPossible(arr, n):
 
    # Stores sum of the array
    S = sum(arr)
 
    # If sum is positive
    if(S >= 0):
        return true
 
    # If sum is negative
    else:
        return false
 
 
# Driver Code
if __name__ == '__main__':
 
    arr = [-5, 3]
    n = len(arr)
 
    # Function call
    if (isPossible(arr, n)):
        print("Yes")
    else:
        print("No")


C#
// C# program for
// the above approach
using System;
class GFG{
   
// Function to check if array sum
// can be reduced to zero or not
static bool isPossible(int[] arr,
                       int n)
{
  // Stores the sum
  // of the array
  int S = 0;
 
  for (int i = 0; i < n; i++)
  {
    S = S + arr[i];
  }
 
  // If array sum is positive
  if (S >= 0)
 
    // Array sum can be
    // reduced to 0
    return true;
 
  // Otherwise
  else
 
    // Array sum cannot
    // be reduced to 0
    return false;
}
 
// Driver Code
public static void Main()
{
  int[] arr = {-5, 3};
  int n = arr.Length;
 
  // Function call
  if (isPossible(arr, n))
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by Chitranayal


输出:
No


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