🚀 Efficient "Number Containers" Using Hash Maps & Ordered Sets! 🔥🔥
IntuitionThe problem requires efficiently updating and retrieving indices associated with numbers. Using a hash map (unordered_map) and ordered set (set) helps
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires efficiently updating and retrieving indices associated with numbers. *Using a hash map (unordered_map) and ordered set (set) helps keep track of these mappings efficiently.*

1. **change**(index, number) should update the mapping of an index to a number while maintaining the sorted order of indices for each number.
2. **find**(number) should return the smallest index associated with the given number or -1 if none exist.

# Approach
<!-- Describe your approach to solving the problem. -->
**Data Structures:**

- unordered_map<int, set<int>> mp: Stores numbers as keys and a sorted set of indices as values.
- unordered_map<int, int> mp2: Maps each index to its corresponding numb

**Operations:**
1. **change(index, number):**
- If the index already exists in mp2, remove it from the old number's set.
- Update mp2[index] to the new number.
- Insert index into mp[number] (ensuring sorted retrieval).
2. **find(number):**
    - If mp[number] is empty, return -1.
    - Otherwise, return the smallest index (the first element in the set).
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
1. **change(index, number): 𝑂(log 𝑁)**
O(logN) (due to set.erase() and set.insert() operations).
2. **find(number): 𝑂(1)**
O(1) (fetching the smallest element in a set is constant time).

- Space complexity: O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# If you found my solution helpful, I'd really appreciate an upvote! 🚀

# Code
```cpp []
class NumberContainers {
    unordered_map<int,set<int>>mp;
    unordered_map<int,int>mp2;
public:
    NumberContainers() {
    }
    
    void change(int index, int number) {
        if(mp2.find(index)!=mp2.end()){
            int num=mp2[index];
            mp[num].erase(index);
        }
        mp2[index]=number;
        mp[number].insert(index);
    }
    
    int find(int number) {
        if(mp[number].size()==0) return -1;
        return *mp[number].begin();
    }
};

/**
 * Your NumberContainers object will be instantiated and called as such:
 * NumberContainers* obj = new NumberContainers();
 * obj->change(index,number);
 * int param_2 = obj->find(number);
 */
```
['Hash Table', 'Ordered Map', 'Ordered Set', 'C++']