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.
I believe the idea is to use a hashmap. Given the index of an ice cream and it's price, you can retrieve the index of another ice cream (if it exists in the map) like this:
maps[price]
and both ice creams will have a combined price of m, giving you the answer. The line:
if( maps.find(v[i]) != maps.end() ){
looks a bit strange but it is actually just the standard way to check whether a key exists in a map. v[i] is the price he is checking.
Having unchecked the price of an ice cream in the map and found nothing, he then adds it to the map a bit like this:
maps[m - price] = index;
so it can be found when checking ice creams further on.
There is one small flaw in the code, and that is the fact that he should use unordered_map instead of map. unordered_map would allow an solution because it uses hashmaps, but map uses a tree based implementation so his solution is really .
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
I believe the idea is to use a hashmap. Given the index of an ice cream and it's price, you can retrieve the index of another ice cream (if it exists in the map) like this:
and both ice creams will have a combined price of m, giving you the answer. The line:
looks a bit strange but it is actually just the standard way to check whether a key exists in a map. v[i] is the price he is checking.
Having unchecked the price of an ice cream in the map and found nothing, he then adds it to the map a bit like this:
so it can be found when checking ice creams further on.
There is one small flaw in the code, and that is the fact that he should use unordered_map instead of map. unordered_map would allow an solution because it uses hashmaps, but map uses a tree based implementation so his solution is really .