Start learning free

LESSON 271 OF 410 · BINARY SEARCH

Two Sum II - O(n log n) Time Complexity in JavaScript

Practice O(n log n) Read the full explanation

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input will have at most one solution, and you may not use the same index twice.

In case no solution exists, return [-1, -1]

Example:

Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1]
Explanation: nums[0] + nums[1] = 2 + 7 = 9

Note:

Your algorithm should run in O(n log n) time and use O(n) extra space.


STEP 1 OF 14
    Ctrl + Enter to check

    JAVASCRIPT · BINARY SEARCH

    Two Sum in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to find two distinct indices in the array such that the sum of the elements at these indices equals the target value. This problem is significant in various applications, such as financial transactions, where you need to find pairs of transactions that sum up to a specific amount.

    Potential pitfalls include assuming multiple solutions or using the same index twice, which the problem explicitly forbids.

    Approach

    To solve this problem, we can use a hash map to store the difference between the target and each element as we iterate through the array. This allows us to check in constant time if the complement of the current element exists in the hash map.

    Let's discuss a naive solution first:

    Naive Solution

    The naive approach involves using two nested loops to check all possible pairs of elements. This solution has a time complexity of O(n^2), which is not optimal for large arrays.

    Optimized Solution

    An optimized solution uses a hash map to store the indices of the elements as we iterate through the array. This approach has a time complexity of O(n) and a space complexity of O(n).

    Algorithm

    Here is a step-by-step breakdown of the optimized algorithm:

    1. Initialize an empty hash map.
    2. Iterate through the array.
    3. For each element, calculate its complement (target - current element).
    4. Check if the complement exists in the hash map.
    5. If it exists, return the indices of the current element and its complement.
    6. If it does not exist, add the current element and its index to the hash map.
    7. If no solution is found, return [-1, -1].

    Code Implementation

    // Function to find two indices that sum up to the target
    function twoSum(nums, target) {
        // Create a hash map to store the indices of the elements
        const map = new Map();
        
        // Iterate through the array
        for (let i = 0; i < nums.length; i++) {
            // Calculate the complement
            const complement = target - nums[i];
            
            // Check if the complement exists in the hash map
            if (map.has(complement)) {
                // Return the indices of the current element and its complement
                return [map.get(complement), i];
            }
            
            // Add the current element and its index to the hash map
            map.set(nums[i], i);
        }
        
        // If no solution is found, return [-1, -1]
        return [-1, -1];
    }
    
    // Example usage
    const nums = [2, 7, 11, 15];
    const target = 9;
    console.log(twoSum(nums, target)); // Output: [0, 1]
    

    Complexity Analysis

    The time complexity of this solution is O(n) because we iterate through the array once. The space complexity is also O(n) due to the hash map storing the indices of the elements.

    Edge Cases

    Potential edge cases include:

    • An empty array: The function should return [-1, -1].
    • An array with one element: The function should return [-1, -1].
    • No two elements sum up to the target: The function should return [-1, -1].

    To test these edge cases, you can use the following examples:

    console.log(twoSum([], 9)); // Output: [-1, -1]
    console.log(twoSum([1], 9)); // Output: [-1, -1]
    console.log(twoSum([1, 2, 3], 7)); // Output: [-1, -1]

    Testing

    To test the solution comprehensively, you should include a variety of test cases, from simple to complex. You can use testing frameworks like Jest or Mocha for automated testing.

    Thinking and Problem-Solving Tips

    When approaching such problems, it's essential to:

    • Understand the problem requirements and constraints.
    • Think about different approaches and their trade-offs.
    • Start with a naive solution and then optimize it.
    • Practice similar problems to improve problem-solving skills.

    Conclusion

    In this blog post, we discussed the Two Sum II problem, its significance, and various approaches to solve it. We provided a detailed explanation of the optimized solution, including its algorithm, code implementation, and complexity analysis. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Additional Resources

    For further reading and practice, you can explore the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free