https://discuss.leetcode.com/topic/17/pairs-across-2-sorted-arrays-with-k-largest-sum
Find the pairs across 2 sorted arrays with K largest sum
ex : // A={1, 2, 4, 5, 6}, B={3, 5, 7, 9} K = 3 result = (1, 3),(2, 3),(1, 5)
You can reuse each individual element from either array, but the pair itself should not be reused.
What if an array contains duplicates?
A = [1,1,2]
B = [1,2,3]
B = [1,2,3]
Should the answer be: (1,1), (1,1), (1,2)?
static class Pair{
int aValue;
int bValue;
int aIndex;
int bIndex;
public Pair(int aValue,int bValue,int aIndex,int bIndex){
this.aValue = aValue;
this.bValue = bValue;
this.aIndex = aIndex;
this.bIndex = bIndex;
}
public boolean equals(Object other){
return ((Pair)other).aValue+((Pair)other).bValue==this.bValue+this.aValue;
}
public String toString(){
return "(" + this.aValue+" , "+this.bValue+")";
}
}
public static void main(String[] args) {
int [] A={1,1, 2, 4, 5, 6};
int [] B={3,3, 5, 7, 9};
int k =3;
ArrayList<Pair> parkedPairs = new ArrayList<Pair>();
int solutionFound =1;
int Aindex =0;
int Bindex = 0;
ArrayList<Pair> solution = new ArrayList<Pair>();
solution.add(new Pair(A[Aindex],B[Bindex],0,0));
while(solutionFound<k){
if(Aindex<A.length && Bindex< B.length ){
int AincrementSolution = A[Aindex+1] + B[Bindex] ;
int BincrementSolution = A[Aindex] + B[Bindex+1] ;
int stackSolution = Integer.MAX_VALUE;
Pair solutionPair =null;
if(!parkedPairs.isEmpty()){
Pair parked = parkedPairs.get(0);
stackSolution = parked.aValue+parked.bValue;
}
if(AincrementSolution <= BincrementSolution && AincrementSolution < stackSolution){
parkedPairs.add(new Pair(A[Aindex],B[Bindex+1],Aindex,Bindex+1));
Aindex = Aindex+1;
solutionPair = new Pair(A[Aindex],B[Bindex],Aindex,Bindex);
}
else{
if(BincrementSolution < AincrementSolution && BincrementSolution < stackSolution){
parkedPairs.add(new Pair(A[Aindex+1],B[Bindex],Aindex+1,Bindex));
Bindex = Bindex+1;
solutionPair = new Pair(A[Aindex],B[Bindex],Aindex,Bindex);
}
else{
solutionPair = parkedPairs.remove(0);
}
}
if(!solution.contains(solutionPair)){
solution.add(solutionPair);
solutionFound++;
}
}
}
class Pair {
int a; int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
public Pair[] KPairs(int[] array1, int[] array2, int n) {
class PairComparator implements Comparator<Pair> {
@Override
public int compare(Pair p1, Pair p2) {
return (array1[p1.a] + array2[p1.b]) - (array1[p2.a] + array2[p2.b]);
}
}
Pair[] result = new Pair[100];
boolean[][] visitedPairs = new boolean[array1.length][array2.length];
PriorityQueue<Pair> pairs = new PriorityQueue<>(array1.length * array2.length, new PairComparator());
pairs.offer(new Pair(0, 0));
visitedPairs[0][0] = true;
for (int position = 0; position < n; position++) {
//poll the minimum element and add to the result
Pair peek = pairs.poll();
//Adding the actual elements
result[position] = new Pair(array1[peek.a], array2[peek.b]);
int i = peek.a;
int j = peek.b;
if (i < array1.length - 1 && !visitedPairs[i + 1][j]) {
pairs.offer(new Pair(i + 1, j));
visitedPairs[i + 1][j] = true;
}
if (j < array2.length - 1 && !visitedPairs[i][j + 1]) {
pairs.offer(new Pair(i, j + 1));
visitedPairs[i][j + 1] = true;
}
}
return result;
}