Check if sum of the given array can be reduced to 0 by reducing array elements by K Last Updated : 08 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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, decreasing two elements at any index by K( = 2), makes the sum of the array 0.Input: arr[ ]= {1, -6, 2, 2}, K=1Output: "No" Approach: Follow the steps below to solve the problem: Traverse the array and calculate the sum of the given array.According to the value of the sum, the following cases arise:If sum = 0: No operation is required. Therefore, the answer is "Yes".If sum > 0: Sum can be reduced to 0 only if sum is a multiple of K. If sum is not a multiple of K, print "No". Otherwise, print "Yes".If sum < 0: Simply print "No". Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to check if the // sum can be made 0 or not int sumzero(int arr[], int N, int K) { // Stores sum of array elements int sum = 0; // Traverse the array for (int i = 0; i < N; i++) { sum += arr[i]; } if (sum == 0) cout << "Yes"; else if (sum > 0) { if (sum % K == 0) cout << "Yes"; else cout << "No"; } else cout << "No"; return 0; } // Driver Code int main() { int K, N; // Given array arr[] int arr1[] = { 1, -6, 2, 2 }; K = 1; N = sizeof(arr1) / sizeof(arr1[0]); sumzero(arr1, N, K); return 0; } Java // Java program for the above approach import java.util.*; class GFG{ // Function to check if the // sum can be made 0 or not static int sumzero(int arr[], int N, int K) { // Stores sum of array elements int sum = 0; // Traverse the array for (int i = 0; i < N; i++) { sum += arr[i]; } if (sum == 0) System.out.print("Yes"); else if (sum > 0) { if (sum % K == 0) System.out.print("Yes"); else System.out.print("No"); } else System.out.print("No"); return 0; } // Driver Code public static void main(String[] args) { int K, N; // Given array arr[] int arr1[] = { 1, -6, 2, 2 }; K = 1; N = arr1.length; sumzero(arr1, N, K); } } // This code is contributed by 29AjayKumar Python3 # Python3 program for the above approach # Function to check if the # sum can be made 0 or not def sumzero(arr, N, K) : # Stores sum of array elements sum = 0; # Traverse the array for i in range(N) : sum += arr[i]; if (sum == 0) : print("Yes"); elif (sum > 0) : if (sum % K == 0) : print("Yes"); else : print("No"); else : print("No"); # Driver Code if __name__ == "__main__" : # Given array arr[] arr1 = [ 1, -6, 2, 2 ]; K = 1; N = len(arr1); sumzero(arr1, N, K); # This code is contributed by AnkThon C# // C# program for the above approach using System; class GFG{ // Function to check if the // sum can be made 0 or not static int sumzero(int []arr, int N, int K) { // Stores sum of array elements int sum = 0; // Traverse the array for (int i = 0; i < N; i++) { sum += arr[i]; } if (sum == 0) Console.Write("Yes"); else if (sum > 0) { if (sum % K == 0) Console.Write("Yes"); else Console.Write("No"); } else Console.Write("No"); return 0; } // Driver Code public static void Main(String[] args) { int K, N; // Given array []arr int []arr1 = { 1, -6, 2, 2 }; K = 1; N = arr1.Length; sumzero(arr1, N, K); } } // This code is contributed by 29AjayKumar JavaScript <script> // JavaScript program for the above approach // Function to check if the // sum can be made 0 or not function sumzero(arr , N , K) { // Stores sum of array elements var sum = 0; // Traverse the array for (i = 0; i < N; i++) { sum += arr[i]; } if (sum == 0) document.write("Yes"); else if (sum > 0) { if (sum % K == 0) document.write("Yes"); else document.write("No"); } else document.write("No"); return 0; } // Driver Code var K, N; // Given array arr var arr1 = [ 1, -6, 2, 2 ]; K = 1; N = arr1.length; sumzero(arr1, N, K); // This code contributed by gauravrajput1 </script> Output: No Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if sum of the given array can be reduced to 0 by reducing array elements by K adarsh_sinhg Follow Improve Article Tags : Greedy Mathematical DSA Arrays array-traversal-question +1 More Practice Tags : ArraysGreedyMathematical Similar Reads Check if sum of array can be reduced to zero by repetitively reducing array element by their index value 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 reduce 5 min read Check if given Array can be reduced to 0 by removing element less than K and adding it to K Given an array, arr[] of size N and an integer K. If a value in arr[] is less than or equal to K, then that value will be removed from the array and added to K. The task is to check if all the elements in arr[] can be absorbed or not. Examples: Input: K = 10, arr[] = {3, 9, 19, 5, 21}Output: trueExp 5 min read Check if all array elements can be made divisible by K by replacing array elements with sum of pairs Given an array arr[] of size N and a positive integer K, the task is to make every element of the array divisible by K by repeatedly selecting a pair of indices (i, j) and perform arr[i] = arr[i] + arr[j]. Examples: Input: arr[] = {3, 6, 3}, K = 6Output: TrueExplanation: Update arr[0] = arr[0] + arr 7 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 Reduce the Array to 0 by decreasing elements by 1 or replacing at most K elements by 0 Given an array arr[] of N integers and a positive integer K, the task is to find the minimum number of operations required to reduce all array elements to 0 such that in each operation reduce any array element by 1 and independently at most K array element can be reduced to 0. Examples: Input: arr[] 6 min read Check if the Array elements can be made 0 with given conditions Given two arrays arr[] and arr2[], and a value K, the task is to check whether the array arr[] can be made to all zeros such that in each operation all elements less than K are made to 0 else they are subtracted by K then the K gets decayed by the smallest value of the array arr2[] whose arr[i] is n 10 min read Sum of all the elements in an array divisible by a given number K Given an array containing N elements and a number K. The task is to find the sum of all such elements which are divisible by K. Examples: Input : arr[] = {15, 16, 10, 9, 6, 7, 17} K = 3 Output : 30 Explanation: As 15, 9, 6 are divisible by 3. So, sum of elements divisible by K = 15 + 9 + 6 = 30. Inp 13 min read Minimum array elements required to be subtracted from either end to reduce K to 0 Given an array arr[] consisting of N integers and an integer K, the task is to reduce K to 0 by removing an array element from either end of the array and subtracting it from K. If it is impossible to reduce K to 0, then print "-1". Otherwise, print the minimum number of such operations required. Ex 9 min read Check if array sum can be made K by three operations on it Given an array A of N integers and a positive integer K. Only three operations can be performed on this array: Replace an integer with the negative value of the integer, Add index number (1-based indexing) of the element to the element itself and Subtract index number of the element from the element 9 min read Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3 7 min read Like