Beats 88% | Explained | Time - O(logm(m+n+logn+n))
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n    int n;\n    int m;\n\n    bool allIndexMarked(int seconds,vector<int>& changeIndices, vector<int>& nums ){\n        vector<int> lastPosition(n+1,-1); // isme hum store krenge ki changeIndeces array me jo index\n                                          // hai unki last occurance kab hai\n        \n        // populate kr dete hai lastPosition ko\n        for(int i=0;i<=seconds;i++){\n            int idx = changeIndices[i];\n            lastPosition[idx] = i+1; // 1 based index ki wjah se yahan b\n        }\n\n        for(int i=1;i<=n;i++){\n            if(lastPosition[i]==-1){ // mtlb ki humara dost aya he nai changeIndex me, mark krne ke liye\n                                     // ana toh pdega he na\n                return false;\n            }\n        }\n\n        map<int,int> positionIndexMap; // dekho ordered map lena b imp tha, taki sorted rahe humare pas cheze\n                                       // (map) on the basis of last seen\n        \n        for(int i=1;i<=n;i++){ // map ko populate krdo\n            int position = lastPosition[i];\n            positionIndexMap[position] = i;\n        }\n\n        int time_passed = 0;\n        for(auto &[position,idx] : positionIndexMap){\n            int time_required = nums[idx-1]+1; // decrement to 0 + 1 time to mark\n\n            if(time_required + time_passed > position) return false;\n\n            time_passed += time_required;\n        }\n\n        return true;\n\n    }\n\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        m = changeIndices.size();\n        n = nums.size();\n\n        int left_time = 0;\n        int right_time = m-1;\n\n        int result = -1;\n\n        while(left_time <= right_time){\n            int mid_time = left_time + (right_time - left_time) / 2;\n            if(allIndexMarked(mid_time,changeIndices, nums )){\n                result = mid_time + 1;\n                right_time = mid_time-1;\n            }else{\n                left_time =mid_time + 1;\n            }\n        }\n\n        return result;\n    }\n};\n```
['C++']