Target Sum Pairs ā Problem Statement & Solution Guide
Problem Description
Given an array of integers nums and a target integer target, return the indices of the two numbers such that they add up to target. Each input has exactly one solution, and you may not use the same element twice.
Examples
Input
[2, 7, 11, 2, 15], 9
Output
[0, 3]
Explanation: Step 1: Sort the input array in ascending order. The array becomes [2, 2, 7, 11, 15]. Step 2: Initialize two pointers, one at the start and one at the end of the array. The start pointer is at index 0 and the end pointer is at index 4. Step 3: Compare the sum of the elements at the start and end pointers with the target. Since 2 + 15 = 17, which equals the target 9, we return [0, 3].
Input
[3, 4, 6, 2], 6
Output
[0, 2]
Explanation: Step 1: Sort the input array in ascending order. The array becomes [2, 3, 4, 6]. Step 2: Initialize two pointers, one at the start and one at the end of the array. The start pointer is at index 0 and the end pointer is at index 3. Step 3: Compare the sum of the elements at the start and end pointers with the target. Since 3 + 6 = 9, which equals the target 6, we return [0, 2].
Constraints
- 2 <= nums.length <= 10^4
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Exactly one valid answer exists.
Optimal Approach & Strategy
The optimal approach is to sort the list of celestial body masses and then use the two pointers technique to find pairs of masses that add up to the target gravitational pull, resulting in a time complexity of O(n log n).
Brute Force Approach
A brute-force approach would involve checking every pair of celestial bodies to see if their masses add up to the target gravitational pull, resulting in a time complexity of O(n²). This approach is inefficient for large lists of celestial bodies.
Verified Code Solutions
function twoSum(nums, target) {
const numMap = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.has(complement)) {
return [numMap.get(complement), i];
}
numMap.set(nums[i], i);
}
return [];
}class Solution {
public int[] twoSum(int[] nums, int target) {
// Create a dictionary to store the elements we've seen so far and their indices
Map<Integer, Integer> num_dict = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
// Calculate the complement of the current number
int complement = target - nums[i];
// Check if the complement is in the dictionary
if (num_dict.containsKey(complement)) {
// If it is, return the indices of the current number and its complement
return new int[] { num_dict.get(complement), i };
}
// If not, add the current number and its index to the dictionary
num_dict.put(nums[i], i);
}
// If we've iterated over the entire array and haven't found a pair of numbers that add up to the target, return an empty array
return new int[] {};
}
}def twoSum(nums, target):
# Create a dictionary to store the elements we've seen so far and their indices
num_dict = {}
for i, num in enumerate(nums):
# Calculate the complement of the current number
complement = target - num
# Check if the complement is in the dictionary
if complement in num_dict:
# If it is, return the indices of the current number and its complement
return [num_dict[complement], i]
# If not, add the current number and its index to the dictionary
num_dict[num] = i
# If we've iterated over the entire array and haven't found a pair of numbers that add up to the target, return an empty list
return []function twoSum(nums, target) {
const numMap = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.has(complement)) {
return [numMap.get(complement), i];
}
numMap.set(nums[i], i);
}
return [];
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.