titan2400

Two Sum - LeetCode

Oct 24th, 2025
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | Source Code | 0 0
  1. // https://leetcode.com/problems/two-sum/description/?source=submission-ac
  2.  
  3. class Solution {
  4.  
  5.     // Time complexity: O(nlogn)
  6.     // Dry runs
  7.     // Example 1: [1, 2, 3, 4, 5], target = 7
  8.     // Example 2: [5, 6, 3, 1] , target = 7
  9.  
  10.     // Below solution works for scenario where we are looking for two numbers that add upto target but not for indices
  11.     // This is because we are sorting and that would change the indices
  12.     // public int[] twoSum(int[] nums, int target) {
  13.     //     Arrays.sort(nums);
  14.     //     int i = 0, j = nums.length - 1;
  15.  
  16.     //     while(i < j) {
  17.     //         if (nums[i] + nums[j] == target) {
  18.     //             return new int[]{nums[i], nums[j]};
  19.     //         }
  20.     //         else if (nums[i] + nums[j] > target) {
  21.     //             j--;
  22.     //         }
  23.     //         else {
  24.     //             i++;
  25.     //         }
  26.     //     }
  27.  
  28.     //     return new int[]{};
  29.        
  30.     // }
  31.  
  32.     // Time complexity: O(n)
  33.     // Space complexity: O(n)
  34.     // I initially though below solution would not work for duplicates however that assumption was wrong
  35.     // public int[] twoSum(int[] nums, int target) {
  36.     //     HashMap<Integer, Integer> map = new HashMap<>();
  37.  
  38.     //     for(int i = 0; i < nums.length; i++) {
  39.     //         map.put(nums[i], i);
  40.     //     }
  41.  
  42.     //     for(int i = 0; i < nums.length; i++) {
  43.     //         if(map.get(target - nums[i]) != null) {
  44.     //             int j = map.get(target - nums[i]);
  45.     //             if (i == j) continue;
  46.     //             return new int[]
  47.     //                 {i, map.get(target - nums[i])};
  48.     //         }
  49.     //     }
  50.  
  51.     //     return new int[]{};
  52.     // }
  53.  
  54.     // Brute force
  55.     // Time Complexity: O(n^2)
  56.     // Space Complexity: O(1)
  57.     // public int[] twoSum(int[] nums, int target) {
  58.     //     for (int i = 0; i < nums.length; i++) {
  59.     //         for (int j = i + 1; j < nums.length; j++) {
  60.     //             if(nums[i] + nums[j] == target) {
  61.     //                 return new int[]{i, j};
  62.     //             }
  63.     //         }
  64.     //     }
  65.  
  66.     //     return new int[]{};
  67.     // }
  68.  
  69.     // Time complexity: O(n)
  70.     // Space complexity: O(n)
  71.     // Two pass hash table
  72.     // public int[] twoSum(int[] nums, int target) {
  73.     //     Map<Integer, Integer> map = new HashMap<>();
  74.     //     for (int i = 0; i < nums.length; i++) {
  75.     //         map.put(nums[i], i);
  76.     //     }
  77.  
  78.     //     for (int i = 0; i < nums.length; i++) {
  79.     //         int complement = target - nums[i];
  80.  
  81.     //         if(map.containsKey(complement) && map.get(complement) != i) {
  82.     //             return new int[] {i, map.get(complement)};
  83.     //         }
  84.     //     }
  85.  
  86.     //     return new int[]{};
  87.     // }
  88.  
  89.     // Time complexity: O(n)
  90.     // Space complexity: O(n)
  91.     // One pass hash table
  92.     public int[] twoSum(int[] nums, int target) {
  93.         Map<Integer, Integer> map = new HashMap<>();
  94.         for (int i = 0; i < nums.length; i++) {
  95.             int complement = target - nums[i];
  96.             if(map.containsKey(complement)) {
  97.                 return new int[]{map.get(complement), i};
  98.             }
  99.  
  100.             map.put(nums[i], i);
  101.         }
  102.  
  103.         return new int[]{};
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment