# Check if sum of the given array can be reduced to 0 by reducing array elements by K

• Last Updated : 08 Apr, 2021

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=2
Output: “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=1
Output: “No”

Approach: Follow the steps below to solve the problem:

1. Traverse the array and calculate the sum of the given array.
2. According to the value of the sum, the following cases arise:
1. If sum = 0: No operation is required. Therefore, the answer is “Yes”.
2. 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”.
3. If sum < 0: Simply print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``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

 ``

Output:

`No`

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

My Personal Notes arrow_drop_up