@@ -24,7 +24,6 @@ of our indices overlap.
24
24
** Time Complexity:** $\mathcal{ O } (N^2)$
25
25
26
26
<LanguageSection >
27
-
28
27
<CPPSection >
29
28
30
29
``` cpp
@@ -63,18 +62,57 @@ int main() {
63
62
int idx = x - v [i ] - v [j ];
64
63
if (hm .find (idx ) != hm .end ()) {
65
64
cout << i + 1 << " " << j + 1 << " " << hm [idx ].f + 1 << " "
66
- << hm [idx ].s + 1 ;
65
+ << hm [idx ].s + 1 << endl ;
67
66
return 0 ;
68
67
}
69
68
}
70
69
71
70
for (int j = i + 1 ; j < n ; j ++) hm[v[i] + v[j]] = {i , j };
72
71
}
73
72
74
- cout << " IMPOSSIBLE\n " ;
73
+ cout << " IMPOSSIBLE" << endl ;
75
74
}
76
75
```
77
76
78
77
</CPPSection>
78
+ <JavaSection>
79
+
80
+ ```java
81
+ import java.io.*;
82
+ import java.util.*;
83
+
84
+ public class Main {
85
+ public static void main(String[] args) {
86
+ Kattio io = new Kattio();
87
+ int n = io.nextInt();
88
+ int x = io.nextInt();
89
+ int[] arr = new int[n + 1]; // 1-indexed array for this problem
90
+ for (int i = 1; i <= n; i++) { arr[i] = io.nextInt(); }
91
+
92
+ Map<Integer, int[]> twoSum = new HashMap<>();
93
+ for (int i = 1; i <= n; i++) {
94
+ for (int j = i + 1; j <= n; j++) {
95
+ int differenceToX = x - arr[i] - arr[j];
96
+ if (twoSum.containsKey(differenceToX)) {
97
+ int k = twoSum.get(differenceToX)[0];
98
+ int l = twoSum.get(differenceToX)[1];
99
+ io.printf("%d %d %d %d", i, j, k, l);
100
+ io.close();
101
+ return;
102
+ }
103
+ }
104
+ for (int j = 1; j <= i - 1; j++) {
105
+ twoSum.put(arr[j] + arr[i], new int[] {j, i});
106
+ }
107
+ }
108
+
109
+ io.println("IMPOSSIBLE");
110
+ io.close();
111
+ }
112
+
113
+ // CodeSnip{Kattio}
114
+ }
115
+ ```
79
116
117
+ </JavaSection >
80
118
</LanguageSection >
0 commit comments