Sorted Two Sum - aloalgo

Sorted Two Sum

Medium

You are given a 0-indexed integer array nums that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Return the indices of the two numbers, index1 and index2, where 1 <= index1 < index2 <= nums.length.

The tests are generated such that there is exactly one solution. You may not use the same element twice. If no result exist, then return the empty array.

Example 1

Inputs
nums = [2, 7, 11, 15]
target = 9
Output
[0, 1]
Explanation:

The numbers 2 and 7 add up to 9. Their 1-based indices are 1 and 2.

Example 2

Inputs
nums = [2, 3, 4]
target = 6
Output
[0, 2]
Explanation:

The numbers 2 and 4 add up to 6. Their 1-based indices are 2 and 3.

Example 3

Inputs
nums = [-1, 0]
target = -1
Output
[0, 1]
Explanation:

The numbers -1 and 0 add up to -1. Their 1-based indices are 1 and 2.

Loading...
Inputs
nums = [2, 7, 11, 15]
target = 9
Output
[0, 1]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!