Binary Search On Answer
Intuition\n Describe your first thoughts on how to solve this problem. \nBinary Search + Map\n\n# Approach\n Describe your approach to solving the problem. \nBi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBinary Search + Map\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBinary Search on Answer and Use Unordered Map and Sliding Window Concept to check, map will store possible nums[i] which can make nums equal(all element in nums=nums[i])\n\n# Complexity\n- Time complexity: O(NlogN)\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)$$ -->\n\n\n# Code\n```\nclass Solution {\n    bool solve(int len,vector<int> &nums){\n        unordered_map<int,int> m;\n        int n=nums.size();\n        m[nums[0]]=1;\n        // if at an index using len our forward and backward elements don\'t overlap\n        // Ex:    _ _ x _ _ for len=2, won\'t overlap\n        // for len>2 simply return true as value at any index can be placed at all indices\n        if(2*len+1<=n){\n            // for index 0 put elements len forward and backward elements in map\n            for(int i=1;i<=len;++i){\n                m[nums[i]]++;\n                m[nums[(n-i)%n]]++;\n            }\n            // then traverse forward and check for every index\n            for(int i=1;i<n;++i){\n                // if front and back point to same element do nothing\n                if(((i+len)%n)==(i-(len+1)+n)%n) continue;\n                // extra element which will become part of range for i\n                int front=nums[(i+len)%n];\n                // lost element which will be removed as we moved ahead\n                int back=nums[(i-(len+1)+n)%n];\n                // add front if it can be used to make all element equal\n                if(m.find(front)!=m.end()) m[front]++;\n                // remove lost element, and if map become empty no such nums[i] for which nums become equal\n                if(m.find(back)!=m.end()){\n                    m[back]--;\n                    if(m[back]==0) m.erase(back);\n                    if(m.size()==0) return false;\n                }\n            }\n            return m.size()>0;\n        } // if at an index using len we can cover whole array length, simply return true \n        else{\n            return true;\n        }\n    }\npublic:\n    int minimumSeconds(vector<int>& nums) {\n        bool flag=true;\n        for(int i=1;i<nums.size();++i){\n            if(nums[i]!=nums[0]){\n                flag=false;\n                break;\n            }\n        }\n        if(flag) return 0;\n        int l=1,r=nums.size();\n        int ans;\n        while(l<=r){\n            int mid=(l+r)>>1;\n            // cout << "mid: " << mid << endl;\n            if(solve(mid,nums)){\n                ans=mid;\n                r=mid-1;\n            } else{\n                l=mid+1;\n            }\n        }\n        return ans;\n    }\n};\n```
['Binary Search', 'Sliding Window', 'Rolling Hash', 'C++']