Sobes.tech
Junior — Senior

Finding indices of two elements with the required sum

livecode

Task condition

Given an array of integers arr and an integer sumTarget, return the indices of two elements that sum up to sumTarget.

Example 1: Input: arr = [2,7,11,15], sumTarget = 9 Output: [0,1] Explanation: arr[0] + arr[1] == 9 → return [0, 1]

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

Example 3: Input: arr = [3,3], sumTarget = 6 Output: [0,1]

class Solution {
public:
    vector<int> twoSum(vector<int>& arr, int sumTarget) {
    }
};