diff --git a/TwoSum.java b/TwoSum.java new file mode 100644 index 00000000..f049854c --- /dev/null +++ b/TwoSum.java @@ -0,0 +1,26 @@ +import java.util.HashMap; + +public class TwoSum { + public int[] twoSum(int[] nums, int target) { + HashMap numToIndex = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int complement = target - nums[i]; + if (numToIndex.containsKey(complement)) { + return new int[] { numToIndex.get(complement), i }; + } + numToIndex.put(nums[i], i); + } + + throw new IllegalArgumentException("No solution found."); + } + + public static void main(String[] args) { + TwoSum solution = new TwoSum(); + int[] nums = {2, 7, 11, 15}; + int target = 9; + int[] result = solution.twoSum(nums, target); + System.out.println("Indices of the two numbers that add up to the target:"); + System.out.println(result[0] + " " + result[1]); + } +}