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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int sumzero( int arr[], int N, int K)
{
int sum = 0;
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;
}
int main()
{
int K, N;
int arr1[] = { 1, -6, 2, 2 };
K = 1;
N = sizeof (arr1) / sizeof (arr1[0]);
sumzero(arr1, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int sumzero( int arr[], int N, int K)
{
int sum = 0 ;
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 ;
}
public static void main(String[] args)
{
int K, N;
int arr1[] = { 1 , - 6 , 2 , 2 };
K = 1 ;
N = arr1.length;
sumzero(arr1, N, K);
}
}
|
Python3
def sumzero(arr, N, K) :
sum = 0 ;
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" );
if __name__ = = "__main__" :
arr1 = [ 1 , - 6 , 2 , 2 ];
K = 1 ;
N = len (arr1);
sumzero(arr1, N, K);
|
C#
using System;
class GFG{
static int sumzero( int []arr, int N, int K)
{
int sum = 0;
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;
}
public static void Main(String[] args)
{
int K, N;
int []arr1 = { 1, -6, 2, 2 };
K = 1;
N = arr1.Length;
sumzero(arr1, N, K);
}
}
|
Javascript
<script>
function sumzero(arr , N , K)
{
var sum = 0;
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;
}
var K, N;
var arr1 = [ 1, -6, 2, 2 ];
K = 1;
N = arr1.length;
sumzero(arr1, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...