给定一个由N 个整数组成的数组arr[] ,任务是通过执行以下任意次数的操作来确定是否可以将数组元素的总和减少到0 :
- 选择一个元素A[i]并将A[i]减少i (基于 1 的索引),任意次数,可能是0 。
- 如果总和可以减少到0 ,则打印“是”。否则,打印“否”。
例子:
Input: arr[] = {2, 3, 1}
Output: Yes
Explanation:
Select A[2] = 3
Perform given operation 3 times to obtain the following result:
3 -> (3 -2) -> (3 – 2 – 2) -> (3 – 2 – 2 – 2)
Sum of the modified array = 2 + (-3) + 1 = 0.
Therefore, the required answer is 0.
Input: arr[] = {-5, 3}
Output: No
方法:这个问题可以基于以下观察来解决:
- 如果一个正数与另一个正数重复相减,那么最终可以得到0 。但是,从负数中重复减去正数,永远无法获得 0,因为它一直在负减少。
- 通过从A[i] 中减去i将总和减少到0的任务。
- 因此,在选择一个元素并将元素的值减少i 时,总和将减少i 。
Let the sum of the array be S.
S = A[1] + A[2] + …. + A[N]
After performing given operations, sum of the array modifies to
S2 = (A[i] – (i)) + (A[i+1] – (i+1)) ….
S2 = A[i] + A[i+1]…. – (i + i+1….)
S2 = S – (i + i + 1…..)
Therefore, after every operation, the original sum is reduced.
- 因此,任务简化为检查数组的初始总和是正数还是0 。如果发现是真的,打印“是”。否则,打印“否”。
下面是上述方法的实现:
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
Javascript
No
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live