O(n) solution looping over array twice
Intuition\nFor each unique number in the ring buffer, we want to find the maximum distance between two copies of that number. If we choose to fill the buffer wi
# Intuition\nFor each unique number in the ring buffer, we want to find the maximum distance between two copies of that number. If we choose to fill the buffer with that number, the number of seconds it takes will be that maximum distance divided by two.\n\nWe can compute this value for each number, then take the minimum among all the possible choices.\n\n# Approach\n1) Build a hashmap mapping each unique number in the array to the maximum distance between two instances of that number\n\n    a) Loop over the ring buffer twice, keeping another hashmap to track the last time we\'ve seen each element. We need to loop twice in order to get the correct values for elements which are unique.\n\n    b) If we hit a number that we\'ve seen before, calculate the distance, and check if it\'s larger than the maximum distance we\'ve seen between instances of that number\n\n2) Go through the hashmap we just built and pick the minimum value across all choices of number to fill the buffer with\n\n3) The minimum time is that number divided by 2 (since we can fill from both sides)\n\n# Complexity\n- Time complexity:\n$O(n)$\n\n- Space complexity:\n$O(n)$\n# Code\n```\nclass Solution {\npublic:\n    void findMaxSeparations(vector<int>& nums, unordered_map<int, int>& distances) {\n        unordered_map<int, int> lastSeen;\n        int n = nums.size();\n\n        for(int i = 0; i < n * 2; i++) {\n            int val = nums[i % n];\n            if (lastSeen.count(val)) { // We\'ve seen this guy before\n                int lastIndex = lastSeen[val];\n                int distance = i - lastIndex;\n                if (distances.count(val)) {\n                    distances[val] = max(distances[val], distance);\n                } else {\n                    distances[val] = distance;\n                }\n            }\n            lastSeen[val] = i;\n        }\n    }\n\n    int minimumSeconds(vector<int>& nums) {\n        unordered_map<int, int> maxSeparations;\n        findMaxSeparations(nums, maxSeparations);\n        int minOfMaxes = INT_MAX;\n        for(auto item : maxSeparations) {\n            if (item.second < minOfMaxes) minOfMaxes = item.second;\n        }\n        return minOfMaxes / 2;\n    }\n};\n```
['C++']