Return the Unique Sorted Target Pair as One-Based Indices

Quick Overview

Find the unique sorted-array target pair with inward pointers and increasing one-based indices.

Return the Unique Sorted Target Pair as One-Based Indices

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem In a nondecreasing sorted array, find the unique pair of different positions whose values sum to `target`. Return the two one-based indices in increasing order. Do not reuse an element, and use only constant extra space. ```cpp vector<int> twoSum(vector<int>& numbers, int target); ``` ### Examples ```text Input: numbers = [-4,-1,2,6,10], target = 5 Output: [2,4] ``` Values -1 and 6 make the target. ```text Input: numbers = [3,3], target = 6 Output: [1,2] ``` Equal values can occupy distinct valid positions. ### Constraints - Length is between 2 and 30000. - Values and target lie between -1000 and 1000. - The array is sorted in nondecreasing order. - Exactly one index-pair solution exists.

Overview: Find the unique sorted-array target pair with inward pointers and increasing one-based indices.

Google logo
Google
Date not reported
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

In a nondecreasing sorted array, find the unique pair of different positions whose values sum to target. Return the two one-based indices in increasing order. Do not reuse an element, and use only constant extra space.

vector<int> twoSum(vector<int>& numbers, int target);

Examples

Input: numbers = [-4,-1,2,6,10], target = 5
Output: [2,4]

Values -1 and 6 make the target.

Input: numbers = [3,3], target = 6
Output: [1,2]

Equal values can occupy distinct valid positions.

Constraints

  • Length is between 2 and 30000.
  • Values and target lie between -1000 and 1000.
  • The array is sorted in nondecreasing order.
  • Exactly one index-pair solution exists.