✅C++\Java/Python ✅Explained all the lines- using unordered_map || time & space -O(n)
Complexity\n- Time complexity:\nO(n) \n\n- Space complexity:\nO(n) \nC++ []\nclass Solution\n{\npublic:\n    int minimumSeconds(vector<int> &nums)\n    {\n     
# Complexity\n- Time complexity:\n$$O(n)$$ \n\n- Space complexity:\n$$O(n)$$ \n```C++ []\nclass Solution\n{\npublic:\n    int minimumSeconds(vector<int> &nums)\n    {\n        // we replace the number at i with i-1 or i+1\n        int n = nums.size();\n        int ans = n - 1; // assuming all numbers are different\n\n        // store all indexs of same number\n        unordered_map<int, vector<int>> m;\n\n        for (int i = 0; i < n; i++)\n            m[nums[i]].push_back(i);\n\n        for (auto i : m) // traverse all the different number\n        {\n            vector<int> v = i.second; // all the indexs of same number\n            int len = v.size();       // size of vector\n            //  now we change all remaining number= total size of array - size of same number\n            int maxi = (n - 1) - (v[len - 1] - v[0]);\n            for (int j = 1; j < len; j++)\n            {\n                // maximum difference btw adjacent index\n                maxi = max(maxi, v[j] - v[j - 1] - 1);\n            }\n            // minimise the maximum distance btw any number\n            ans = min(ans, maxi);\n        }\n        // we traverse in both direction at same time so we give half of the answer\n        return (ans + 1) / 2;\n    }\n};\n```\n```python []\nclass Solution(object):\n    def minimumSeconds(self, nums):\n        # we replace the number at i with i-1 or i+1\n        n = len(nums)\n        ans = n - 1  # assuming all numbers are different\n\n        # store all indexes of the same number\n        m = {}\n\n        for i, num in enumerate(nums):\n            if num not in m:\n                m[num] = []\n            m[num].append(i)\n\n        for v in m.values():  # traverse all the different numbers\n            v.sort()  # Sort the indexes in ascending order\n            len_v = len(v)  # size of vector\n            # now we change all remaining number = total size of the array - size of the same number\n            maxi = (n - 1) - (v[len_v - 1] - v[0])\n            for j in range(1, len_v):\n                # maximum difference between adjacent indexes\n                maxi = max(maxi, v[j] - v[j - 1] - 1)\n            # minimize the maximum distance between any numbers\n            ans = min(ans, maxi)\n\n        # we traverse in both directions at the same time, so we give half of the answer\n        return (ans + 1) // 2\n```\n```Java []\nclass Solution {\n   public int minimumSeconds(List<Integer> nums) {\n        // we replace the number at i with i-1 or i+1\n        int n = nums.size();\n        int ans = n - 1; // assuming all numbers are different\n\n        // store all indexes of the same number\n        Map<Integer, List<Integer>> m = new HashMap<>();\n\n        for (int i = 0; i < n; i++) {\n            if (!m.containsKey(nums.get(i))) {\n                m.put(nums.get(i), new ArrayList<>());\n            }\n            m.get(nums.get(i)).add(i);\n        }\n\n        for (Map.Entry<Integer, List<Integer>> entry : m.entrySet()) { // traverse all the different numbers\n            List<Integer> v = entry.getValue(); // all the indexes of the same number\n            int len = v.size(); // size of list\n            // now we change all remaining numbers = total size of the array - size of the same number\n            int maxi = (n - 1) - (v.get(len - 1) - v.get(0));\n            for (int j = 1; j < len; j++) {\n                // maximum difference between adjacent indexes\n                maxi = Math.max(maxi, v.get(j) - v.get(j - 1) - 1);\n            }\n            // minimize the maximum distance between any numbers\n            ans = Math.min(ans, maxi);\n        }\n\n        // we traverse in both directions at the same time, so we give half of the answer\n        return (ans + 1) / 2;\n    }\n}    \n```\n![LEETCODE.png](https://assets.leetcode.com/users/images/37b84a70-d9bd-483e-8d95-a0fa791bb674_1691304287.100432.png)\n
['Ordered Map', 'Python', 'C++', 'Java']