text: Count pairs ( i , j ) from arrays arr [ ] & brr [ ] such that arr [ i ] | Java 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
code: import java . util . * ; import java . io . * ; 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 ] ; } HashMap < Integer , Integer > freqCount = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! freqCount . containsKey ( C [ i ] ) ) freqCount . put ( C [ i ] , 1 ) ; else freqCount . put ( C [ i ] , freqCount . get ( C [ i ] ) + 1 ) ; } int NoOfPairs = 0 ; for ( Map . Entry < Integer , Integer > x : freqCount . entrySet ( ) ) { int y = x . getValue ( ) ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } System . out . println ( 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 ) ; } }