text: Count pairs ( i , j ) from arrays arr [ ] & brr [ ] such that arr [ i ] | C ++ program for the above approach ; 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 ; Driver Code ; Given array arr [ ] and brr [ ] ; Size of given array ; Function calling
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; int CountPairs ( int * a , int * b , int n ) { int C [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { C [ i ] = a [ i ] + b [ i ] ; } map < int , int > freqCount ; for ( int i = 0 ; i < n ; i ++ ) { freqCount [ C [ i ] ] ++ ; } int NoOfPairs = 0 ; for ( auto x : freqCount ) { int y = x . second ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } cout << NoOfPairs ; } int main ( ) { int arr [ ] = { 1 , 4 , 20 , 3 , 10 , 5 } ; int brr [ ] = { 9 , 6 , 1 , 7 , 11 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; CountPairs ( arr , brr , N ) ; return 0 ; }