Skip to content

Can anyone explain me this question: Two Sum #11

Answered by bibhuti9
bibhutigswain asked this question in Q&A
Discussion options

You must be logged in to vote

The given problem is attempting to solve the classic "Two Sum" problem using JavaScript. In the "Two Sum" problem, you are given an array of integers (nums) and a target integer (target). Your goal is to find two different indices in the array such that the numbers at those indices add up to the target. You should return these indices in an array.

Here’s the provided code and an explanation of why it doesn’t work as intended:

var twoSum = function (nums, target) {
    let numIndices = {};  // Create a hash map to store the indices of elements
    for (let i = 0; i < nums.length; i++) {
        let complement = target - nums[i];  // Calculate the complement
        if (numIndices.hasOwnPro…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bibhuti9
Comment options

Answer selected by bibhuti9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants