We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Thanks for the supplement. I solved it this way though I have a doubt. This code will not return correct value for following data set

11672785838

Correct result should be 3,5 while this will return 3,7. Although none of the TC has this sort of data, this should be tackled in the code. What do you say?

My hash impl returned 3,5. Hash impl is O(n) space and time. BS impl is O(nlogn) time, O(n) space. Justifying a sub-optimal answer as a "learning experience" without providing a progression of improvements seems poor form, and would not suffice in an interview or in the real world imo.

Eg : 2 2 3 4
here, while inserting in map, as 2 has already been insertes, so while at index 1 , u checked whether 2 + 2 == money, otherwise did,t considered it ??

## Hash Tables: Ice Cream Parlor

You are viewing a single comment's thread. Return to all comments →

[two-sum](https://leetcode.com/articles/two-sum/)

Thanks for the supplement. I solved it this way though I have a doubt. This code will not return correct value for following data set

Correct result should be 3,5 while this will return 3,7. Although none of the TC has this sort of data, this should be tackled in the code. What do you say?

HashMap in java does not let you enter duplicate keys. So in your map, the key 8 is associated with index 6

If there is only one 8 in the coins and the money is 16, will the hashMap implementation give the right answer? Since the flavours need to be distinct

My hash impl returned 3,5. Hash impl is O(n) space and time. BS impl is O(nlogn) time, O(n) space. Justifying a sub-optimal answer as a "learning experience" without providing a progression of improvements seems poor form, and would not suffice in an interview or in the real world imo.

Did you handled the duplicacy case, as:

Eg : 2 2 3 4 here, while inserting in map, as 2 has already been insertes, so while at index 1 , u checked whether 2 + 2 == money, otherwise did,t considered it ??

it is mentioned that there will be only unique solution as part of the constraints, so your test case is not correct it should not have 2 solutions

Thanks for pointing that out. I just can't find my solution to fix the test case...

There is a unique solution...it is mentioned in the problem

https://web.stanford.edu/class/cs9/sample_probs/TwoSum.pdf

This is a very useful resource. Thanks!