From 5ec4423b0366e866799088be2243cd197ac18ca4 Mon Sep 17 00:00:00 2001 From: sandy7907 Date: Sun, 22 Feb 2026 00:53:29 -0500 Subject: [PATCH] FAANMG | Competetive Coding - 2 --- TwoSum.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 TwoSum.java diff --git a/TwoSum.java b/TwoSum.java new file mode 100644 index 00000000..f6fddb39 --- /dev/null +++ b/TwoSum.java @@ -0,0 +1,17 @@ +class TwoSum { + public int[] twoSum(int[] nums, int target) { + + HashMap valueDiffMap = new HashMap<>(); + + for(int i = 0; i < nums.length;i++) { + int diff = target - nums[i]; + + if(valueDiffMap.containsKey(diff)) { + return new int[]{valueDiffMap.get(diff), i}; + } else { + valueDiffMap.put(nums[i], i); + } + } + return new int[] {-1, -1}; + } +} \ No newline at end of file