c++ : Easy to Understand with Intuition, comments & images | memory beats : 93.58%
Intuition\n Describe your first thoughts on how to solve this problem. \nAbbreviation :\nBy "given number" means : number that is passed to function as paramter
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Abbreviation :**\n**By "given number" means : number that is passed to function as paramter.\nBy "given index" means : index that is passed to function as paramter.**\n\nSo as soon as i read that i have to return the smallest indices of number (indices because same no. can be inserted at many index),\ni understood that i have to create an key-value unordered map,\nin that i can store the indices of number.\nExample:\n> number[10] -> indices[3,5,8]\n\nBut the case arrive when given number has to be inserted at given index where another number already exist.\nfor example : \n>     number 10 is at indices 1,3,5\n>     now insert 20 at index 1\n\n**Now there can be three cases for existed value/number in this :** \n1. Where it has only 1 index, means it only exist in one. index.\n    \n    ![image.png](https://assets.leetcode.com/users/images/f4bf3e25-d67a-427e-9b08-f1454a2a9c72_1677022831.2942252.png)\n\n2.  Where it has multiple indices:\n    - given index is smallest for existing number.\n    ![image.png](https://assets.leetcode.com/users/images/64e960a8-8432-4139-8d5c-6e7bc52512a4_1677022577.9675019.png)\n    - given index is not smallest for existing number\n    ![image.png](https://assets.leetcode.com/users/images/f4b886d0-7af4-46d1-8f13-65c3d00fa85b_1677022748.9980266.png)\n\n\n\n---\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI created an Index to number key-value unordered map,\nso that i can keep track of what value/number is at what index.\n> key[index] -> value[number]\n\nthen created an Number to indices key-value unordered map,\nso that i can keep track of what value/number is exists/present at what index.\n\n- ***but here\'s the catch,***\n\nstore these indices in a order set, why ?\n    so that the smallest index is always at the beginning of set\n\nso now every "key"{number} will have an {order set} as their "value"\n> key[number] -> value[set[index]]\n\n\n---\n\n\n\n\n# Complexity\n# - Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nerase method of set takes $$O(n)$$ time complex. .\ninsert method of set takes $$O(log(n))$$ time complex. .\nfind method of unordered_map can take $$O(1)$$ time complex. in average case but $$O(n)$$ time complex. in worst case .\n\n![image.png](https://assets.leetcode.com/users/images/30b493cc-b9d5-4692-9d43-19b7a524e609_1677024204.6087408.png)\nimage from "cplusplus.com"\n\n**So overall Time Complexity becomes : $$O(n)$$**\n\n\n# - Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nMainly i am creating these two maps :\n> unordered_map<int, set<int>> container\nunordered_map<int,int> ind\n\ni think Overall Space Complexity is : $$O(m * n)$$ \nm is total unique numbers,\nn is indices\n*(not so sure about this)*\n\n\n\n# Code\n```\nclass NumberContainers {\npublic:\n    // To store key-values of number to indices\n    unordered_map<int, set<int>> container;\n\n    // To store key-values of index to number\n    unordered_map<int,int> ind;\n\n    NumberContainers() {\n\n        // initializing both of them\n        container = unordered_map<int, set<int>>();\n        ind = unordered_map<int,int>();\n\n    }\n    \n    void change(int index, int number) {\n\n        // checking if any value exist at given index or not\n        // and if that existed value is equal to given number or not\n        // if all this condition is true then\n        //      erase given index from set of container\n        if(ind[index] != 0 && ind[index] != number)\n            container[ind[index]].erase(index);\n\n        // inserting/updating index & number\n        ind[index] = number;\n        container[number].insert(index);\n\n    }\n    \n    int find(int number) {\n\n        int res = -1; // initializing result\n\n        // checking if given number exist or not\n        if(container.find(number) != container.end()){\n\n            // get the first value/index from given number\'s set\n            // we will automatically get the smallest index as it is sorted\n            res = *(container[number].begin());\n\n            // fact : if set of *number* is empty then\n            //        begin() of set will return 0 \n            if(res == 0)\n                res = -1;\n\n        }\n\n        return res;\n    }\n};\n \n```
['Hash Table', 'Sorting', 'Ordered Set', 'C++']