-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoSum.java
30 lines (27 loc) · 871 Bytes
/
twoSum.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
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return new int[]{}; // No solution found
}
}
//test case
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = solution.twoSum(nums, target);
if (result.length == 2) {
System.out.println("Indices of numbers that add up to the target: " + result[0] + " and " + result[1]);
} else {
System.out.println("No solution found.");
}
}
}