-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq16p21.java
More file actions
34 lines (32 loc) · 948 Bytes
/
Copy pathq16p21.java
File metadata and controls
34 lines (32 loc) · 948 Bytes
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
import java.util.*;
public class q16p21{
public static void main(String[] args){
int[] inputA = {4,1,2,1,1,2};
int[] inputB = {3,6,3,3};
System.out.println(find(inputA, inputB));
}
// T: O(nm)
// another way, b = a - target
// and save b in hashset.
// T should be O(n+m)
public static List<Integer> find(int[] inputA, int[] inputB){
List<Integer> li = new ArrayList<>();
int target = (sum(inputA) - sum(inputB)) / 2;
for(int i = 0; i<inputA.length; i++){
for(int j = 0; j<inputB.length; j++){
if(inputA[i] - inputB[j] == target){
li.add(inputA[i]);
li.add(inputB[j]);
return li;
}
}
}
return li;
}
public static int sum(int[] arr){
int ans = 0;
for(int i : arr)
ans += i;
return ans;
}
}