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:
- 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 notint 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 Codeint 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 approachimport java.util.*;class GFG{// Function to check if the// sum can be made 0 or notstatic 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 Codepublic 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 notdef 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 Codeif __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 approachusing System;class GFG{// Function to check if the// sum can be made 0 or notstatic 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 Codepublic 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> |
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.


