Count of elements which is the sum of a subarray of the given Array | Function to count the elements such that their exist a subarray whose sum is equal to this element ; Loop to count the frequency ; Loop to iterate over every possible subarray of the array ; Driver Code
function countElement ( arr , n ) { let freq = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { let tmpsum = arr [ i ] ; for ( let j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( tmpsum <= n ) { ans += freq [ tmpsum ] ; freq [ tmpsum ] = 0 ; } } } return ans ; } let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; let n = arr . length ; document . write ( countElement ( arr , n ) ) ;