-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
58 lines (49 loc) · 1.53 KB
/
Solution.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
import java.io.*;
import java.util.*;
class PairEntry implements Comparable<PairEntry>{
public int value;
public int index;
public PairEntry(int v, int i) {
value = v;
index = i;
}
public int compareTo(PairEntry b) {
return (this.value >= b.value) ? 1: -1;
}
}
class Pair{
public int first;
public int second;
public Pair(int f, int s) { first = f; second = s; }
}
public class Solution {
public static Pair find(int m, ArrayList<PairEntry> arr) {
Collections.sort(arr);
Pair result = new Pair(-1, -1);
int i = 0, j = arr.size() - 1;
while (i != j && (arr.get(i).value + arr.get(j).value != m)) {
if (arr.get(i).value + arr.get(j).value > m) {
j--;
} else {
i++;
}
}
result.first = Math.min(arr.get(i).index, arr.get(j).index) + 1;
result.second = Math.max(arr.get(i).index, arr.get(j).index) + 1;
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int m = sc.nextInt();
int n = sc.nextInt();
ArrayList<PairEntry> arr = new ArrayList<PairEntry>();
for (int j = 0; j < n; j++) {
arr.add(j, new PairEntry(sc.nextInt(), j));
}
Pair result = Solution.find(m, arr);
System.out.println(result.first + " " + result.second);
}
}
}