Count array elements that can be represented as sum of at least two consecutive array elements | Function to find the number of array elements that can be represented as the sum of two or more consecutive array elements ; Stores the frequencies of array elements ; Stores required count ; Update frequency of each array element ; Find sum of all subarrays ; Increment ans by cnt [ sum ] ; Reset cnt [ sum ] by 0 ; Return ans ; Driver Code ; Given array ; Function call
def countElements ( a , n ) : NEW_LINE INDENT cnt = [ 0 ] * ( n + 1 ) NEW_LINE ans = 0 NEW_LINE for k in a : NEW_LINE INDENT cnt [ k ] += 1 NEW_LINE DEDENT for l in range ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for r in range ( l , n ) : NEW_LINE INDENT sum += a [ r ] NEW_LINE if ( l == r ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( sum <= n ) : NEW_LINE INDENT ans += cnt [ sum ] NEW_LINE cnt [ sum ] = 0 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 1 , 1 , 1 , 1 ] NEW_LINE print ( countElements ( a , len ( a ) ) ) NEW_LINE DEDENT