Written this 2sums code to get efficent O(N)
Time complexity algorithm for below problem
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
unfortunately saw value at array nums
got displayed in the output ,whereas the need is to get the indices
to be displayed in output
What change needs to be done below
let hashTwoSum = (array, sum) => {
let numsObj = {}
let nums = []
for(let i in array){
let addend = sum - array[i]
if (addend in numsObj){
nums.push([addend, array[i]])
}
numsObj[array[i]] = i
}
return nums
}
let array = [2,7,11,15]
console.log(hashTwoSum(array,9))
Your help is appreciated
Regards,
Carolyn