Maximizing Unique Pairs from two arrays
Given two arrays of equal size N, form maximum number of pairs by using their elements, one from the first array and second from the second array, such that an element from each array is used at-most-once and the absolute difference between the selected elements used for forming a pair is less than or equal to a given element K.
Examples:
Input : a[] = {3, 4, 5, 2, 1}
b[] = {6, 5, 4, 7, 15}
k = 3
Output : 4
The maximum number of pairs that can be formed
using the above 2 arrays is 4 and the corresponding
pairs are [1, 4], [2, 5], [3, 6], [4, 7], we can't
pair the remaining elements.
Other way of pairing under given constraint is
[2, 5], [3, 6], [4, 4], but count of pairs here
is 3 which is less than the result 4.
Simple Approach: By taking few examples, we can observe that if we sort both arrays. Then one by picking the closest feasible element for every element, we get the optimal answer.
In this approach we first sort both the arrays and then compare each element of the first array with each element of the second array for the possible pair, if it’s possible to form a pair, we form the pair and move to check for the next possible pair for the next element of the first array.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll findMaxPairs(ll a[], ll b[], ll n, ll k)
{
sort(a, a+n);
sort(b, b+n);
bool flag[n];
memset (flag, false , sizeof (flag));
int result = 0;
for ( int i=0; i<n; i++)
{
for ( int j=0; j<n; j++)
{
if ( abs (a[i]-b[j])<=k && flag[j]== false )
{
result++;
flag[j] = true ;
break ;
}
}
}
return result;
}
int main()
{
ll a[] = {10, 15, 20}, b[] = {17, 12, 24};
int n = sizeof (a)/ sizeof (a[0]);
int k = 3;
cout << findMaxPairs(a, b, n, k);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int findMaxPairs( int a[], int b[], int n, int k)
{
Arrays.sort(a);
Arrays.sort(b);
boolean []flag = new boolean [n];
Arrays.fill(flag, false );
int result = 0 ;
for ( int i= 0 ; i<n; i++)
{
for ( int j= 0 ; j<n; j++)
{
if (Math.abs(a[i]-b[j])<=k && flag[j]== false )
{
result++;
flag[j] = true ;
break ;
}
}
}
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));
}
}
|
Python 3
def findMaxPairs(a, b, n, k):
a.sort()
b.sort()
flag = [ False ] * n
result = 0
for i in range (n):
for j in range (n):
if ( abs (a[i] - b[j]) < = k and
flag[j] = = False ):
result + = 1
flag[j] = True
break
return result
if __name__ = = "__main__" :
a = [ 10 , 15 , 20 ]
b = [ 17 , 12 , 24 ]
n = len (a)
k = 3
print (findMaxPairs(a, b, n, k))
|
C#
using System;
class GFG
{
static int findMaxPairs( int []a, int []b,
int n, int k)
{
Array.Sort(a);
Array.Sort(b);
bool []flag = new bool [n];
int result = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if (Math.Abs(a[i] - b[j]) <= k && flag[j] == false )
{
result++;
flag[j] = true ;
break ;
}
}
}
return result;
}
public static void Main()
{
int [] a = {10, 15, 20};
int [] b = {17, 12, 24};
int n = a.Length;
int k = 3;
Console.WriteLine(findMaxPairs(a, b, n, k));
}
}
|
Javascript
<script>
function findMaxPairs(a,b,n,k)
{
a.sort( function (c,d){ return c-d;});
b.sort( function (c,d){ return c-d;})
let flag = new Array(n);
for (let i=0;i<flag.length;i++)
{
flag[i]= false ;
}
let result = 0;
for (let i=0; i<n; i++)
{
for (let j=0; j<n; j++)
{
if (Math.abs(a[i]-b[j])<=k && flag[j]== false )
{
result++;
flag[j] = true ;
break ;
}
}
}
return result;
}
let a=[10, 15, 20];
let b=[17, 12, 24];
let n = a.length;
let k = 3;
document.write(findMaxPairs(a, b, n, k));
</script>
|
Time complexity : O(n2)
Auxiliary Space : O(n)
Efficient Approach: In this approach, rather than checking all the possible combinations of pairs, we optimize our code by checking only the feasible combination of pairs using the 2 pointer approach.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
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;
}
|
Java
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));
}
}
|
Python3
def findMaxPairs(a,b,n,k):
a.sort()
b.sort()
result = 0
j = 0
for i in range (n):
if j<n:
if abs (a[i] - b[j])< = k:
result + = 1
j + = 1
elif a[i]>b[j]:
j + = 1
return result
if __name__ = = '__main__' :
a = [ 10 , 15 , 20 ]
b = [ 17 , 12 , 24 ]
n = len (a)
k = 3
print (findMaxPairs(a,b,n,k))
|
C#
using System;
class GFG
{
static int findMaxPairs( int []a, int []b,
int n, int k)
{
Array.Sort(a);
Array.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;
Console.WriteLine(findMaxPairs(a, b, n, k));
}
}
|
Javascript
<script>
function findMaxPairs(a, b, n, k)
{
a.sort( function (a, b){ return a - b});
b.sort( function (a, b){ return a - b});
let result = 0;
for (let 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;
}
let a = [10, 15, 20];
let b = [17, 12, 24];
let n = a.length;
let k = 3;
document.write(findMaxPairs(a, b, n, k));
</script>
|
Time complexity : O(n Log n)
Auxiliary Space : O(1)
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...