Check if sum of the given array can be reduced to 0 by reducing array elements by K | Java 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 [ ]
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 ) ; } }