-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairSum.java
More file actions
68 lines (38 loc) · 1.17 KB
/
Copy pathPairSum.java
File metadata and controls
68 lines (38 loc) · 1.17 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package arrays;
import java.util.Arrays;
public class PairSum {
public static void pairSum1(int[] arr, int num) {
Arrays.sort(arr);
int l = 0, h = arr.length - 1;
while (l < h) {
if (arr[l] + arr[h] == num) {
int end = h;
while (arr[l] + arr[end--] == num) {
System.out.println(arr[l] + " " + arr[h]);
}
l++;
} else if (arr[l] + arr[h] > num) {
h--;
}
else {
l++;
}
}
}
public static void pairSum2(int[] input, int x) {
for (int i = 0; i < input.length - 1; i++) {
for (int j = i + 1; j < input.length; j++)
{
if (input[i] + input[j] == x) {
if (input[i] < input[j]) {
System.out.println(input[i] + " " + input[j]);
} else {
System.out.println(input[j] + " " + input[i]);
}
}
}
}
}
public static void main(String[] args) {
}
}