Hashmap of Vectors | C++
Approach\nFoe each unique value in the array calculate the number of seconds it takes for it to make the whole array equal to that value. To do this in the most
# Approach\nFoe each unique value in the array calculate the number of seconds it takes for it to make the whole array equal to that value. To do this in the most optimized way, we can store the indices of each unique value somwhere and then try to calculate how many seconds will it require for it to make the array equal.\n\nIf an element is present at index $$i$$ and index $$j$$ then it will take a maximum of $$(j-i)/2$$ seconds to make all the elements between those indices equal to this element. Iterate therough the vector and find out the maximum seconds between each gap (as this happens simulataneously so no need to add the seconds) and at the end return the minimum out of answers for all unique elements.\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n# Code\n```\n#include<bits/stdc++.h>\nclass Solution {\npublic:\n    int minimumSeconds(vector<int>& nums) {\n        unordered_map<int,vector<int>>freq;\n        int n=nums.size();\n        for(int i=0;i<nums.size();++i)\n            freq[nums[i]].push_back(i);\n        int mini = INT_MAX;\n        for(auto [k,v]:freq){\n            int t = 0;\n            for(int i=1;i<v.size();++i)\n                t = max(t, (v[i] - v[i-1])/2);\n            t = max(t, ((n - v.back() - 1) + v[0] + 1)/2);\n            mini = min(mini,t);\n        }\n        return mini;\n    }\n};\n```
['Hash Table', 'C++']