diff --git a/Problem1.java b/Problem1.java index e69de29b..8a9c8f14 100644 --- a/Problem1.java +++ b/Problem1.java @@ -0,0 +1,22 @@ +//Two Sum problem +// Time Complexity : O(n). +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Approach : As we have to check if the target can be achieved with addition of two numbers in the Array, I have calculated the difference by subtracting +// target and nums[i]. Then we check in the HashMap if the differnce is present, if not we add to the HashMap. + + +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap map = new HashMap<>(); + for(int i=0; i