-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
27 lines (25 loc) · 729 Bytes
/
TwoSum.java
File metadata and controls
27 lines (25 loc) · 729 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
public class TwoSum {
public static void main(String[] args) {
int [] array = {5, 2, 4};
int target = 6;
twoSum(array, target);
}
private static void twoSum(int[] array, int target) {
int [] ans = new int [2];
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if((array[i] + array[j]) == target){
ans[0] = i;
ans[1] = j;
}
}
}
printArray(ans);
}
private static void printArray(int[] inputArray) {
for (int j : inputArray) {
System.out.print(j + " ");
}
System.out.println();
}
}