Check if sum of the given array can be reduced to 0 by reducing array elements by K | 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 [ ]
def sumzero ( arr , N , K ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT elif ( sum > 0 ) : NEW_LINE INDENT if ( sum % K == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , - 6 , 2 , 2 ] ; NEW_LINE K = 1 ; NEW_LINE N = len ( arr1 ) ; NEW_LINE sumzero ( arr1 , N , K ) ; NEW_LINE DEDENT