Check if sum of array can be reduced to zero by repetitively reducing array element by their index value Last Updated : 27 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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] = 3Perform 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"; } } Java // Java program for the 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 <script> // javascript program for the above approach // Function to check if array sum // can be reduced to zero or not function isPossible(arr , n) { // Stores the sum of the array var S = 0; for (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 var arr = [ -5, 3 ]; var n = arr.length; // Function call if (isPossible(arr, n)) document.write("Yes"); else document.write("No"); // This code is contributed by todaysgaurav </script> Output: No Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if all elements of the given array can be made 0 by decrementing value in pairs S sameerbamanha Follow Improve Article Tags : Mathematical DSA Arrays array-rearrange array-traversal-question +1 More Practice Tags : ArraysMathematical Similar Reads Check if sum of the given array can be reduced to 0 by reducing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to check if the sum of the array can be reduced to 0 by subtracting array elements by K any number of times. Examples: Input: arr[ ]= {-3, 2, -1, 5, 1}, K=2Output: "Yes"Explanation: Sum of the array is 4. Therefore, decreasi 4 min read Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum Given an array arr[] consisting of N integers, the task is to check if it is possible to reduce the array elements to 0 by repeatedly subtracting the minimum of any pair of consecutive array elements from both the elements in the pair. If it is possible, then print "Yes". Otherwise, print "No". Exam 10 min read Check if sum of array can be made equal to X by removing either the first or last digits of every array element Given an array arr[] consisting of N positive integers and a positive integer X, the task is to check if the sum of the given array can be made equal to X by removing either the first or last digit of every array element. If it is possible to make the sum of array elements equal to X, then print "Ye 7 min read Check whether each Array element can be reduced to minimum element by replacing it with remainder with some X Given an array arr[] of non-negative integers, the task is to check whether all array elements can be reduced to the minimum element in the array by choosing any positive integer X and update an array element arr[i] to arr[i]%X. If it is possible to do so, then print Yes. Otherwise, print No. Exampl 6 min read Check if all elements of the given array can be made 0 by decrementing value in pairs Given an array arr[] consisting of positive integers, the task is to check if all elements of the given array can be made 0 by performing the following operation: Choose two indices i and j such that i != j and subtract 1 from both arr[i] and arr[j]The above operation can be performed any number of 6 min read Count ways to split array into two equal sum subarrays by replacing each array element to 0 once Given an array arr[] consisting of N integers, the task is to count the number of ways to split the array into two subarrays of equal sum after changing a single array element to 0. Examples: Input: arr[] = {1, 2, -1, 3}Output: 4Explanation: Replacing arr[0] by 0, arr[] is modified to {0, 2, -1, 3}. 11 min read Like