Given an array **arr[]** consisting of **N** integers, the task is to determine if the sum of array elements can be reduced to **0** by performing the following operations any number of times:

- Choose an element
**A[i]**and reduce**A[i]**by**i**(*1-based indexing*), any number of times, possibly**0**. - If the sum can be reduced to
**0**, print “**Yes**“. Otherwise, print “**No**“.

**Examples:**

Input:arr[] = {2, 3, 1}Output:YesExplanation:

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

**Approach: **This problem can be solved based on the following observations:

- If a
**positive number**is repetitively subtracted from another**positive number**, then eventually,**0**can be obtained. But, repetitively subtracting a**positive number**from a**negative number**, 0 can never be obtained as it keeps on decreasing negatively. - The task to reduce the sum to
**0**by subtracting**i**from**A[i]**. - Therefore, on choosing an element and reducing the value of the element by
**i**, the**sum**is being reduced by**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.

- Therefore, the task reduces to checking if the initial sum of the array is positive or
**0**. If found to be true, print “**Yes**“. Otherwise, print “**No**“.

Below is the implementation for the above approach:

## C++

`// C++ Program for the above approach` `#include <iostream>` `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"` `;` ` ` `}` `}` |

*chevron_right*

*filter_none*

## 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"` `);` ` ` `}` `}` |

*chevron_right*

*filter_none*

## 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"` `)` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

No

**Time Complexity: **O(N) **Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Find the index which is the last to be reduced to zero after performing a given operation
- Count the numbers that can be reduced to zero or less in a game
- Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Find maximum value of the last element after reducing the array with given operations
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Maximize cost to empty given array by repetitively removing K array elements
- Check if A can be converted to B by reducing with a Prime number
- Modify given array by reducing each element by its next smaller element
- Largest number N which can be reduced to 0 in K steps
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Find last remaining element after reducing the Array
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Maximize sum of array by reducing array elements to contain no triplets (i, j, k) where a[i] < a[j] and a[i] < a[k] and j <i < k
- Queries to minimize sum added to given ranges in an array to make their Bitwise AND non-zero
- Maximum of sum and product of digits until number is reduced to a single digit
- Sum of given N fractions in reduced form
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.