Count pairs ( i , j ) from arrays arr [ ] & brr [ ] such that arr [ i ] | C # program for the above approach ; Function to find the minimum number needed to be added so that the sum of the digits does not exceed K ; 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 map < int , int > freqCount ; ; 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
using System ; using System . Collections . Generic ; class GFG { static void CountPairs ( int [ ] a , int [ ] b , int n ) { int [ ] C = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { C [ i ] = a [ i ] + b [ i ] ; } Dictionary < int , int > freqCount = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! freqCount . ContainsKey ( C [ i ] ) ) freqCount . Add ( C [ i ] , 1 ) ; else freqCount [ C [ i ] ] = freqCount [ C [ i ] ] + 1 ; } int NoOfPairs = 0 ; foreach ( KeyValuePair < int , int > x in freqCount ) { int y = x . Value ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } Console . WriteLine ( NoOfPairs ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 20 , 3 , 10 , 5 } ; int [ ] brr = { 9 , 6 , 1 , 7 , 11 , 6 } ; int N = arr . Length ; CountPairs ( arr , brr , N ) ; } }