Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can anyone explain me this question: Two Sum #11

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

Can anyone explain me this Two Sum DSA question

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

I also have same question @bibhuti9

You must be logged in to vote
1 reply
Comment options

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.hasOwnProperty(complement)) { // Check if complement exists in hash map
 return [numIndices[complement], i]; // Return the indices if found
 }
 numIndices[nums[i]] = i; // Store the index of the current element in the hash map
 }
 return []; // Return an empty array if no solution is found
};
var nums = [3, 2, 4], target = 6;
console.log(twoSum(nums, target)); // Output: [1, 2]

Explanation

Initialization: We initialize an empty object numIndices to store the indices of the elements.

Iterate through the array: We loop through each element in the array nums.

Calculate complement: For each element, we calculate the complement (i.e., target - nums[i]).

Check for complement: We check if the complement exists in the hash map numIndices. If it does, we return the indices of the complement and the current element.

Store the current element index: If the complement does not exist, we store the index of the current element in the hash map.

Return result: If we find a pair that sums up to the target, we return their indices. If no such pair is found, we return an empty array.

This approach ensures that we find the indices efficiently with a time complexity of
O
(
n
)
O(n), where
n
n is the length of the input array nums.

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

AltStyle によって変換されたページ (->オリジナル) /