Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ques::Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.  You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.


Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].


What I have tried:

This is the correct solution to the question above.
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> mp;
        int n= nums.size();
        
        for(int i=0; i<n; i++){
            if (mp.find(target-nums[i]) != mp.end()) 
                return {mp[target-nums[i]],i};
            
            mp[nums[i]]=i;
        }
        return {};
    }
};

i m not able to understand how its working for every single iteration. can someone explain through dry run the given solution.
Posted
Updated 25-Aug-22 2:37am
Comments
merano99 25-Aug-22 12:53pm    
You write "thankyou! got it" but so far you have neither given stars nor marked posts as a solution. It would be nice if you still do that. Or what would you still be missing?

It builds a map with nums' (value, index) pairs.
Before adding the i-th pair, it checks if there is a matching value in the already constructed map: if it is so then it returns the corresponding indices. On the other hand, if the iteration is completed, then no matching values were found hence it return the empty vector.
 
Share this answer
 
Comments
merano99 25-Aug-22 8:38am    
correct, +5
CPallini 25-Aug-22 8:40am    
Thank you.
Bharti Saiinii 25-Aug-22 10:50am    
thankyou!! got it
CPallini 25-Aug-22 11:01am    
You are welcome.
Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Comments
CPallini 25-Aug-22 8:41am    
5.
Bharti Saiinii 26-Aug-22 6:53am    
ok i will be specific from next time
CPallini has already explained it correctly. I'll try to describe it a little differently.

The values in the vector nums are looped through. In an initially empty map, a value is searched for which, together with the current value from nums as a sum, corresponds to the searched value target.
If no value matching the sum is found, the current value from nums is stored with its index in the map. If the current value from nums is suitable, a vector is created from the index from the map and the current index from nums and returned.
 
Share this answer
 
Comments
CPallini 25-Aug-22 8:41am    
5.
Bharti Saiinii 25-Aug-22 10:51am    
ok got it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900