Dodomeki
Sign in

11. Two Sum in a Sorted Array

Easy

Given a sorted array numeros in increasing order and an integer alvo, return the 1-based indices of two values whose sum is alvo. There is exactly one solution, and the same element may not be used twice.

Example 1
Input: numeros = [1, 4, 6, 10], alvo = 10
Output: [2, 3]
Explanation: The values 4 and 6 sum to 10.
Example 2
Input: numeros = [-3, 0, 5, 9], alvo = 6
Output: [1, 4]
Constraints
  • 2 <= numeros.length <= 10^5
  • numeros está em ordem crescente.
  • Existe exatamente uma resposta.
ArraysTwo pointers
Caso 1
numeros =
[1,4,6,10]
alvo =
10
Expected =
[2,3]
Caso 2
numeros =
[-3,0,5,9]
alvo =
6
Expected =
[1,4]

Run checks these 2 cases. Submit runs all 6, including hidden ones.