-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIceCreamHash.cpp
More file actions
34 lines (31 loc) · 1001 Bytes
/
Copy pathIceCreamHash.cpp
File metadata and controls
34 lines (31 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
// Complete the whatFlavors function below.
// https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem
void whatFlavors(vector<int> cost, int money) {
// map <int, int> umap;
// for(int i = 0; i < cost.size(); i++)
// {
// int complement = money - cost[i];
// if(umap[complement])
// cout << i << endl;
// else
// umap[cost] = complement;
// }
map<int, int> mymap;
// Fill the map with costs and corresponding indeces
for(int i = 0; i < cost.size(); i++){
mymap[cost[i]] = i;
}
//iterate over the cost array and find complements
for(int i = 0; i < cost.size(); i++){
int complement = money - cost[i];
if(mymap.find(complement) != mymap.end() && mymap[complement] != i){
cout << i + 1 << " " << mymap[complement] + 1 << endl;
return;
}
}
}