Count pairs ( i , j ) from arrays arr [ ] & brr [ ] such that arr [ i ] | Function to count the pairs such that given condition is satisfied ; Stores the sum of element at each corresponding index ; Find the sum of each index of both array ; Stores frequency of each element present in sumArr ; Initialize number of pairs ; Add possible vaid pairs ; Return Number of Pairs ; Given array arr [ ] and brr [ ] ; Size of given array ; Function calling
function CountPairs ( a , b , n ) { var C = Array ( n ) ; for ( var i = 0 ; i < n ; i ++ ) { C [ i ] = a [ i ] + b [ i ] ; } var freqCount = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( freqCount . has ( C [ i ] ) ) freqCount . set ( C [ i ] , freqCount . get ( C [ i ] ) + 1 ) else freqCount . set ( C [ i ] , 1 ) } var NoOfPairs = 0 ; freqCount . forEach ( ( value , key ) => { var y = value ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } ) ; document . write ( NoOfPairs ) ; } var arr = [ 1 , 4 , 20 , 3 , 10 , 5 ] ; var brr = [ 9 , 6 , 1 , 7 , 11 , 6 ] ; var N = arr . length ; CountPairs ( arr , brr , N ) ;