-
Notifications
You must be signed in to change notification settings - Fork 19
/
closestsums.java
64 lines (49 loc) · 1.32 KB
/
closestsums.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class closestsums {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int cases = 0;
while (scan.hasNextInt())
{
cases++;
System.out.println("Case " + cases + ":");
int n = scan.nextInt();
int[] nums = new int[n];
for (int i = 0; i < nums.length; i++)
nums[i] = scan.nextInt();
ArrayList<Integer> sums = new ArrayList<>((n * (n + 1)) / 2);
for (int i = 0; i < nums.length - 1; i++)
for (int j = i + 1; j < nums.length; j++)
if (nums[i] != nums[j])
sums.add(nums[i] + nums[j]);
Collections.sort(sums);
int m = scan.nextInt();
int[] queries = new int[m];
for (int i = 0; i < queries.length; i++)
queries[i] = scan.nextInt();
int closest = 0;
for (int query : queries)
{
int index = Collections.binarySearch(sums, query);
if (index >= 0)
closest = sums.get(index);
else
{
index = -index - 1;
if (index == 0 || index == sums.size())
closest = sums.get(index == 0 ? 0 : sums.size() - 1);
else
{
int a1 = sums.get(index - 1);
int a2 = sums.get(index);
closest = Math.abs(query - a1) <= Math.abs(query - a2) ? a1 : a2;
}
}
System.out.println("Closest sum to " + query + " is " + closest + ".");
}
}
scan.close();
}
}