Sobes.tech
Junior — Senior

Determine the indices of pairs of elements whose sum equals a given number

livecode

Task condition

Given two input parameters: an array of integers numbers and an integer targetSum. Find two distinct indices i and j in the array numbers such that numbers[i] + numbers[j] == targetSum, and return them as an array [i, j]. If no such pair exists, return an empty array or null depending on the requirements.

Example:

numbers = [2, 7, 11, 15]
targetSum = 9
# Expected result: [0, 1] because numbers[0] + numbers[1] = 2 + 7 = 9

Requirements:

  • The solution should have a time complexity of O(n) or better.
  • Any correct implementation in the chosen language is acceptable.