Number of subarrays having sum exactly equal to k | Python3 program for the above approach ; Calculate all subarrays ; Calculate required sum ; Check if sum is equal to required sum
arr = [ 10 , 2 , - 2 , - 20 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = - 10 NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT summ += arr [ j ] NEW_LINE if summ == k : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT print ( res ) NEW_LINE