Two Sum III in C++ (O(n log n) Time Complexity)


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.


Understanding the Problem

The core challenge of the "Two Sum III" problem is to find two distinct indices in an array such that the numbers at those indices add up to a given target. 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 consider multiple approaches:

Naive Solution

The naive solution involves checking all pairs of numbers to see if they sum up to the target. This approach has a time complexity of O(n^2), which is not optimal for large arrays.

Optimized Solution Using Hash Map

A more efficient approach uses a hash map to store the indices of the numbers we have seen so far. This allows us to check in constant time if the complement of the current number (i.e., target - current number) exists in the hash map.

Optimized Solution Using Sorting and Two Pointers

Another approach involves sorting the array and using two pointers to find the two numbers that sum up to the target. This approach has a time complexity of O(n log n) due to the sorting step.

Algorithm

Let's break down the hash map approach step-by-step:

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

Code Implementation

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

vector<int> twoSum(vector<int>& nums, int target) {
    // Create a hash map to store the indices of the numbers
    unordered_map<int, int> num_map;
    
    // Iterate through the array
    for (int i = 0; i < nums.size(); ++i) {
        int complement = target - nums[i];
        
        // Check if the complement exists in the hash map
        if (num_map.find(complement) != num_map.end()) {
            // If found, return the indices
            return {num_map[complement], i};
        }
        
        // Add the current number and its index to the hash map
        num_map[nums[i]] = i;
    }
    
    // If no solution is found, return [-1, -1]
    return {-1, -1};
}

int main() {
    vector<int> nums = {2, 7, 11, 15};
    int target = 9;
    vector<int> result = twoSum(nums, target);
    
    cout << "Indices: [" << result[0] << ", " << result[1] << "]" << endl;
    
    return 0;
}

Complexity Analysis

The time complexity of the hash map approach 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.

In comparison, the sorting and two pointers approach has a time complexity of O(n log n) due to the sorting step and a space complexity of O(1) if sorting is done in place.

Edge Cases

Potential edge cases include:

Testing

To test the solution comprehensively, consider the following test cases:

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed the "Two Sum III" problem, explored various approaches to solve it, and provided a detailed explanation of the hash map solution. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

We encourage you to practice and explore further to enhance your understanding and proficiency.

Additional Resources

For further reading and practice, consider the following resources: