Count of elements which is the sum of a subarray of the given Array | Function to count element such that their exist a subarray whose sum is equal to this element ; Loop to compute frequency of the given elements ; Loop to iterate over every possible subarray of array ; Driver Code
def countElement ( arr , n ) : NEW_LINE INDENT freq = { } NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] = freq . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT tmpsum = arr [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT tmpsum += arr [ j ] NEW_LINE if tmpsum in freq : NEW_LINE INDENT ans += freq [ tmpsum ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countElement ( arr , n ) ) NEW_LINE DEDENT