FastPrepTwo Sum with an Ordered Index Pair

Two Sum with an Ordered Index Pair

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

For this exercise, assume you are given an integer array nums and an integer target. Return two distinct indices [i,j] with i < j such that nums[i] + nums[j] == target.

For this exercise, assume that when several pairs work, choose the pair with the smallest first index. If those first indices are equal, choose the smallest second index. Return an empty array when no pair works, including when fewer than two numbers are supplied.

Repeated values may be used if they occupy different indices. Do not reorder or modify the input array.

Function

twoSumPair(nums: int[], target: int) → int[]

Examples

Example 1

nums = [2,7,11,15]target = 9return = [0,1]

Indices 0 and 1 contain 2 and 7, whose sum is 9.

Example 2

nums = [3,3,3]target = 6return = [0,1]

Repeated values are allowed at distinct indices. Among the three valid pairs, [0,1] is first.

Example 3

nums = [1,4,6,9]target = 10return = [0,3]

Both [0,3] and [1,2] sum to 10. Choose [0,3] because its first index is smaller.

Constraints

  • For this exercise, assume 0 <= nums.length <= 500.
  • For this exercise, assume -10^9 <= nums[i] <= 10^9 and -2 * 10^9 <= target <= 2 * 10^9.

More Atlassian problems

See Atlassian hiring insights
public int[] twoSumPair(int[] nums, int target) {
    // Write your code here.
    return new int[0];
}
nums[2,7,11,15]
target9
expected[0,1]
Checking account…