CPP | Easy | Briefly Explained | Two maps ✔
We are mapping smallest index to it\'s value in hashmap mp1\n* We are mapping that value to all it\'s indices placed in set using hashmap mp\n\nNow lets take an
* We are mapping smallest index to it\'s value in hashmap mp1\n* We are mapping that value to all it\'s indices placed in set using hashmap mp\n\nNow lets take an examplechange:[2,10]\nNow we have in hashmap mp1 as [2:10] 2 as a key(smallest index) and 10 as a value and in hashmap mp as [10:[2]]\nchange:[3,10]\nNow we have in hashmap mp1 as [2:10] 2 as a key(smallest index) and 10 as a value and in hashmap mp as [10:[2,3]]\nchange:[2,7] Now here we are required to update the index two to a new value so first we will check if smallest index is already holding some value or not .\nIf it\'s holding then we will delete the index from the set of that value .\nFirst we have 2 exists as an index.\ncheck the value corresponding to this index using hashmap mp1 so we have 10 as value\nnow remove 2 from the set of indices for 10\nNow add [2:7,3:10] in hashmap mp1 and add 2 in the set of indices for value 7\nso in hashmap mp we have [10:[3],7:[2]]\nUpvote if you liked it !!\n\n```\nclass NumberContainers {\npublic:\n    unordered_map<int,set<int>> mp;\n    unordered_map<int,int> mp1;\n    NumberContainers() {\n        \n    }\n    void change(int idx, int n) {\n        if(mp1.count(idx)){\n            int val=mp1[idx];\n            mp[val].erase(idx);\n            if(!mp[val].empty())\n                mp1[*mp[val].begin()]=val;\n        }\n        mp1[idx]=n;\n        mp[n].insert(idx);\n    }\n    \n    int find(int n) {\n        return mp[n].empty()?-1:*mp[n].begin();\n    }\n};\n```
['C']