Maximizing Unique Pairs from two arrays | Java program for Maximizing Unique Pairs from two arrays ; Returns count of maximum pairs that caan be formed from a [ ] and b [ ] under given constraints . ; Sorting the first array . ; Sorting the second array . ; Increasing array pointer of both the first and the second array . ; Increasing array pointer of the second array . ; Increasing array pointer of the first array . ; Driver code
import java . util . * ; class GFG { static int findMaxPairs ( int a [ ] , int b [ ] , int n , int k ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; int result = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { if ( Math . abs ( a [ i ] - b [ j ] ) <= k ) { result ++ ; i ++ ; j ++ ; } else if ( a [ i ] > b [ j ] ) j ++ ; else i ++ ; } return result ; } public static void main ( String args [ ] ) { int a [ ] = { 10 , 15 , 20 } ; int b [ ] = { 17 , 12 , 24 } ; int n = a . length ; int k = 3 ; System . out . println ( findMaxPairs ( a , b , n , k ) ) ; } }