-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairedNArray.java
More file actions
28 lines (24 loc) · 1018 Bytes
/
Copy pathPairedNArray.java
File metadata and controls
28 lines (24 loc) · 1018 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
public class PairedNArray {
public static void main(String[] args) {
System.out.println(isPairedN(new int[] {1, 4, 1, 4, 5, 6}, 5));
System.out.println(isPairedN(new int[] {1, 4, 1, 4, 5, 6}, 6));
System.out.println(isPairedN(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8}, 6));
System.out.println(isPairedN(new int[] {1, 4, 1}, 5));
System.out.println(isPairedN(new int[] {8, 8, 8, 8, 7, 7, 7}, 15));
System.out.println(isPairedN(new int[] {8, -8, 8, 8, 7, 7, -7}, -15));
System.out.println(isPairedN(new int[] {3}, 3));
System.out.println(isPairedN(new int[] {}, 0));
}
static int isPairedN(int[] a, int n) {
if (a.length <= 1 || n <= 0 || n >= (a.length - 1) * 2)
return 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] + a[j] == n && i + j == n) {
return 1;
}
}
}
return 0;
}
}