1. Leetcode: Two Sum

1. Leetcode: Two Sum

Given an array of integers nums and an integer target, return 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. You can return the answer in any order.

Example 1:
Input: nums = [3,2,4], target = 6
Output: [1,2]


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSum {
    // O(n^2) time complexity, O(1) space
    public static int[] twoSum1(int[] nums, int target) {
        for(int i = 0; i < nums.length; i++) {
            for(int j = i+1; j < nums.length; j++) {

                if(nums[i] + nums[j] == target) {
                    return new int[]{i,j};
                }
            }
        }
        return new int[]{};
    }
                 
    // O(n) time complexity, O(n) space
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if(map.containsKey(complement)) {
                int a = i;
                int b = map.get(complement);
                return new int[]{a, b};
            }
            map.put(nums[i], i);    // takes care of duplicates since we make use of the first duplicate even before we replace it
        }
        return new int[]{};
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3, 2, 4};
        int target = 6;
        int[] resultIndexes = twoSum(nums, target);

        System.out.println("Indexes: " + Arrays.toString(resultIndexes));
    }

}

 

Complexity Analysis:

For the method twoSum():

    1. Time Complexity: O(n), where is the length of nums[]. We loop through the input once
    2. Space Complexity: O(n). Map would contain up to n entries, where n is the length of nums[]

 

Leetcode Problem Link:

https://leetcode.com/problems/two-sum/

Designed to make awesome website for any business.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce scelerisque iaculis libero, ac pellentesque mi fermentum vitae. Donec ipsum tellus, dapibus at blandit sed, iaculis quis est. Maecenas luctus facilisis lacus, et aliquet arcu cursus vitae. Nulla dolor erat, iaculis in gravida quis, pellentesque sed tortor. Aliquam varius euismod orci venenatis tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis vel pharetra metus, ut pretium mi. Continue Reading

Codes are optimized to reduce load time.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce scelerisque iaculis libero, ac pellentesque mi fermentum vitae. Donec ipsum tellus, dapibus at blandit sed, iaculis quis est. Maecenas luctus facilisis lacus, et aliquet arcu cursus vitae. Nulla dolor erat, iaculis in gravida quis, pellentesque sed tortor. Aliquam varius euismod orci venenatis tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis vel pharetra metus, ut pretium mi. Continue Reading

We build scope that respect your money.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce scelerisque iaculis libero, ac pellentesque mi fermentum vitae. Donec ipsum tellus, dapibus at blandit sed, iaculis quis est. Maecenas luctus facilisis lacus, et aliquet arcu cursus vitae. Nulla dolor erat, iaculis in gravida quis, pellentesque sed tortor. Aliquam varius euismod orci venenatis tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis vel pharetra metus, ut pretium mi. Continue Reading

Design with the user in mind to enjoy the clean look.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce scelerisque iaculis libero, ac pellentesque mi fermentum vitae. Donec ipsum tellus, dapibus at blandit sed, iaculis quis est. Maecenas luctus facilisis lacus, et aliquet arcu cursus vitae. Nulla dolor erat, iaculis in gravida quis, pellentesque sed tortor. Aliquam varius euismod orci venenatis tincidunt. Interdum et malesuada fames ac ante ipsum primis in faucibus. Duis vel pharetra metus, ut pretium mi. Continue Reading