Sobes.tech
Junior — Senior

Finding pairs of indices with a given sum

livecode

Task condition

Given an array of integers arr and an integer targetSum, return the indices of two elements such that their sum is equal to targetSum.

It is guaranteed that there is exactly one solution for each input array, and the same element cannot be used twice.

The order of the indices in the returned array does not matter.

Example 1:

Input: arr = [2, 7, 11, 15], targetSum = 9
Output: [0, 1]
Explanation: arr[0] + arr[1] == 9, so we return [0, 1].

Example 2:

Input: arr = [3, 2, 4], targetSum = 6
Output: [1, 2]