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
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll findMaxPairs ( ll a [ ] , ll b [ ] , ll n , ll k ) { sort ( a , a + n ) ; sort ( b , b + n ) ; int result = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { if ( abs ( a [ i ] - b [ j ] ) <= k ) { result ++ ; i ++ ; j ++ ; } else if ( a [ i ] > b [ j ] ) j ++ ; else i ++ ; } return result ; } int main ( ) { ll a [ ] = { 10 , 15 , 20 } ; ll b [ ] = { 17 , 12 , 24 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 3 ; cout << findMaxPairs ( a , b , n , k ) ; return 0 ; }