BackmediumArraysPayPalFlipkart

Target Sum Pairs Solution

Problem Statement

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.

Example 1
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].

Example 2
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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Target Sum Pairs — Problem Statement & Solution Guide

ArraysMediumTwo Pointers
TimeO(n)
|
SpaceO(1)

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

Example 1

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].

Example 2

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

JavaScript Solution
Time: O(n)
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

PayPalFlipkart

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.