Numbers of pairs from an array whose average is also present in the array | Function to count the number of pairs from the array having sum S ; Stores the total count of pairs whose sum is 2 * S ; Generate all possible pairs and check their sums ; If the sum is S , then increment the count ; Return the total count of pairs ; Function to count of pairs having whose average exists in the array ; Initialize the count ; Use set to remove duplicates ; Add elements in the set ; For every sum , count all possible pairs ; Return the total count ; Driver code
function getCountPairs ( arr , N , S ) { let count = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { for ( let j = i + 1 ; j < arr . length ; j ++ ) { if ( ( arr [ i ] + arr [ j ] ) == S ) count ++ ; } } return count ; } function countPairs ( arr , N ) { let count = 0 ; let S = [ ] ; for ( let i = 0 ; i < N ; i ++ ) S . push ( arr [ i ] ) ; for ( let ele in S ) { let sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } let arr = [ 4 , 2 , 5 , 1 , 3 , 5 ] ; let N = arr . length ; document . write ( countPairs ( arr , N ) ) ;