Check if sum of the given array can be reduced to 0 by reducing array elements by K | C ++ program for the above approach ; Function to check if the sum can be made 0 or not ; Stores sum of array elements ; Traverse the array ; Driver Code ; Given array arr [ ]
#include <bits/stdc++.h> NEW_LINE 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 ; }