Grokking Microsoft Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Anyone can explain why the return is [-1,-1] here?function pair_with_target_sum...

ornella

Mar 9, 2022

Anyone can explain why the return is [-1,-1] here?

function pair_with_target_sum(arr, targetSum) { const nums = {}; // to store numbers and their indices for (let i = 0; i < arr.length; i++) { const num = arr[i]; if (targetSum - num in nums) { return [nums[targetSum - num], i]; } nums[arr[i]] = i; } return [-1, -1]; }

0

0

Comments
Comments
Design Gurus
Design Gurus4 years ago

[-1, -1] is returned when we can't find a pair in the given array whose sum is equal to the given target.

On this page