forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNMaxCombinations.java
More file actions
50 lines (37 loc) · 1.08 KB
/
NMaxCombinations.java
File metadata and controls
50 lines (37 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Heaps;
import java.util.*;
/**
* Author - archit.s
* Date - 31/10/18
* Time - 11:33 PM
*/
public class NMaxCombinations {
public ArrayList<Integer> solve(ArrayList<Integer> A, ArrayList<Integer> B) {
PriorityQueue<Integer> p = new PriorityQueue<>();
A.sort((x, y) -> y - x);
B.sort((x, y) -> y - x);
for(int a: A){
for(int b: B){
int sum = a+b;
if(p.size()<A.size()){
p.offer(sum);
}
else{
if(sum > p.peek()){
p.poll();
p.offer(sum);
}
else{
break;
}
}
}
}
ArrayList<Integer> r = new ArrayList<>(p);
r.sort((x, y) -> y - x);
return r;
}
public static void main(String[] args) {
System.out.println(new NMaxCombinations().solve(new ArrayList<>(Arrays.asList(1,4,2,3)), new ArrayList<>(Arrays.asList(2,5,1,6))));
}
}