[C++ / Java] Greedy - Find Maximum Distance between Adjacent Occurrences
Code\nC++ []\nclass Solution {\npublic:\n    int minimumSeconds(vector<int>& nums) {\n        int dist = 1e8, n = size(nums);\n        unordered_map<int,vector<
## Code\n```C++ []\nclass Solution {\npublic:\n    int minimumSeconds(vector<int>& nums) {\n        int dist = 1e8, n = size(nums);\n        unordered_map<int,vector<int>> mp;\n        \n        // Storing the occurrences (indices) of each number in hashtable\n        for(int i = 0; i < n; i++){\n            int val = nums[i];\n            mp[val].push_back(i);\n        }\n        \n        // For each number, we find the maximum distance between\n        // any two of its adjacent occurrences\n        for(auto &[k,v] : mp){\n            int sz = size(v), maxi = 0;\n            for(int i = 0; i < sz; i++){\n                int curr = 0;\n                if(i == sz - 1) curr = n - 1 - v.back() + v[0];\n                else curr = v[i + 1] - v[i] - 1;\n                maxi = max(maxi, curr);\n            }\n            // Update minimum distance\n            dist = min(dist, maxi);\n        }\n        \n        // Extra second for odd distance\n        return dist / 2 + (dist % 2);\n        \n    }\n};\n```\n```Java []\npublic class Solution {\n    public int minimumSeconds(List<Integer> nums) {\n        int dist = (int)1e8, n = nums.size();\n        Map<Integer, List<Integer>> mp = new HashMap<>();\n        \n        // Storing the occurrences (indices) of each number in a hashmap\n        for (int i = 0; i < n; i++) {\n            int val = nums.get(i);\n            if (!mp.containsKey(val)) {\n                mp.put(val, new ArrayList<>());\n            }\n            mp.get(val).add(i);\n        }\n        \n        // For each number, we find the maximum distance between any two of its adjacent occurrences\n        for (Map.Entry<Integer, List<Integer>> entry : mp.entrySet()) {\n            List<Integer> v = entry.getValue();\n            int sz = v.size(), maxi = 0;\n            for (int i = 0; i < sz; i++) {\n                int curr = 0;\n                if (i == sz - 1) {\n                    curr = n - 1 - v.get(sz - 1) + v.get(0);\n                } else {\n                    curr = v.get(i + 1) - v.get(i) - 1;\n                }\n                maxi = Math.max(maxi, curr);\n            }\n            // Update minimum distance\n            dist = Math.min(dist, maxi);\n        }\n        \n        // Extra second for odd distance\n        return dist / 2 + (dist % 2);\n    }\n}\n```\n\n## Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->
['Hash Table', 'Greedy', 'C++', 'Java']