-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeb_4_TwoSum.java
More file actions
38 lines (31 loc) · 1.13 KB
/
Feb_4_TwoSum.java
File metadata and controls
38 lines (31 loc) · 1.13 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
package Daily_Problem;
import java.util.HashMap;
import java.util.Map;
public class Feb_4_TwoSum {
public static void main(String[] args) {
Feb_4_TwoSum solution = new Feb_4_TwoSum();
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = Feb_4_TwoSum.twoSum(nums, target);
if (result[0] != 0 || result[1] != 0) {
System.out.println("Indices of the two numbers that add up to the target:");
System.out.println("Index 1: " + result[0] + ", Index 2: " + result[1]);
} else {
System.out.println("No solution found.");
}
}
public static int[] twoSum(int[] nums, int target) {
int n = nums.length;
Map<Integer, Integer> map = new HashMap<>();
int[] result = new int[2];
for (int i = 0; i < n; i++) {
if (map.containsKey(target - nums[i])) {
result[1] = i;
result[0] = map.get(target - nums[i]);
return result;
}
map.put(nums[i], i);
}
return result;
}
}