From 3c185200a233d3e0c6ce07210ac3231fc038ba6d Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Tue, 9 Jun 2026 18:37:21 +0530 Subject: [PATCH 1/2] Add Two Sum solution in Java (kumar1593#12) Implement Two Sum problem using HashMap for efficient lookup. --- Problems/Arrays/two_sum.java | 61 ++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Problems/Arrays/two_sum.java diff --git a/Problems/Arrays/two_sum.java b/Problems/Arrays/two_sum.java new file mode 100644 index 0000000..7e0e20a --- /dev/null +++ b/Problems/Arrays/two_sum.java @@ -0,0 +1,61 @@ +/** + * Problem: Two Sum (Issue #12) + * + * Given an array of integers nums and an integer target, return the indices + * of the two numbers such that they add up to target. + * You may assume that each input would have exactly one solution, and you + * may not use the same element twice. + * + * Approach: HashMap for O(n) lookup + * - Iterate through the array + * - For each element, check if (target - element) exists in the map + * - If yes, return the stored index and the current index + * - Otherwise, store the element and its index in the map + * + * Time Complexity: O(n) + * Space Complexity: O(n) + * + * Test Cases: + * nums = [2, 7, 11, 15], target = 9 -> [0, 1] + * nums = [3, 2, 4], target = 6 -> [1, 2] + * nums = [3, 3], target = 6 -> [0, 1] + */ + +import java.util.Arrays; +import java.util.HashMap; + +public class two_sum { + + public int[] twoSum(int[] nums, int target) { + // Map stores: value -> index + HashMap map = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + int complement = target - nums[i]; + + if (map.containsKey(complement)) { + // Found the pair + return new int[]{map.get(complement), i}; + } + + // Store current element with its index + map.put(nums[i], i); + } + + // Problem guarantees a solution exists, so this won't be reached + throw new IllegalArgumentException("No two sum solution found."); + } + + public static void main(String[] args) { + two_sum solution = new two_sum(); + + // Test case 1: Expected [0, 1] + System.out.println(Arrays.toString(solution.twoSum(new int[]{2, 7, 11, 15}, 9))); + + // Test case 2: Expected [1, 2] + System.out.println(Arrays.toString(solution.twoSum(new int[]{3, 2, 4}, 6))); + + // Test case 3: Expected [0, 1] + System.out.println(Arrays.toString(solution.twoSum(new int[]{3, 3}, 6))); + } +} From 307d70886dd37ebdd08141197f6f6a78727b9a3b Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Tue, 9 Jun 2026 23:31:31 +0530 Subject: [PATCH 2/2] Fix: rename class to TwoSum following Java naming conventions Renamed two_sum.java to TwoSum.java and updated the public class name from 'two_sum' to 'TwoSum' to follow standard Java PascalCase naming conventions. Also updated the instantiation in main() accordingly. --- Problems/Arrays/{two_sum.java => TwoSum.java} | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) rename Problems/Arrays/{two_sum.java => TwoSum.java} (77%) diff --git a/Problems/Arrays/two_sum.java b/Problems/Arrays/TwoSum.java similarity index 77% rename from Problems/Arrays/two_sum.java rename to Problems/Arrays/TwoSum.java index 7e0e20a..dc63c80 100644 --- a/Problems/Arrays/two_sum.java +++ b/Problems/Arrays/TwoSum.java @@ -7,24 +7,24 @@ * may not use the same element twice. * * Approach: HashMap for O(n) lookup - * - Iterate through the array - * - For each element, check if (target - element) exists in the map - * - If yes, return the stored index and the current index - * - Otherwise, store the element and its index in the map + * - Iterate through the array + * - For each element, check if (target - element) exists in the map + * - If yes, return the stored index and the current index + * - Otherwise, store the element and its index in the map * - * Time Complexity: O(n) + * Time Complexity: O(n) * Space Complexity: O(n) * * Test Cases: - * nums = [2, 7, 11, 15], target = 9 -> [0, 1] - * nums = [3, 2, 4], target = 6 -> [1, 2] - * nums = [3, 3], target = 6 -> [0, 1] + * nums = [2, 7, 11, 15], target = 9 -> [0, 1] + * nums = [3, 2, 4], target = 6 -> [1, 2] + * nums = [3, 3], target = 6 -> [0, 1] */ import java.util.Arrays; import java.util.HashMap; -public class two_sum { +public class TwoSum { public int[] twoSum(int[] nums, int target) { // Map stores: value -> index @@ -47,7 +47,7 @@ public int[] twoSum(int[] nums, int target) { } public static void main(String[] args) { - two_sum solution = new two_sum(); + TwoSum solution = new TwoSum(); // Test case 1: Expected [0, 1] System.out.println(Arrays.toString(solution.twoSum(new int[]{2, 7, 11, 15}, 9)));