Simple Binary Search Solution in CPP
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int m, n;
    bool isValid(int seconds, vector<int>& changeIndices, vector<int>& nums) {
        
        vector<int>lastPosition(n+1, -1); //to make it one indexed;
        for (int i = 0; i <= seconds; i++) {
            int idx = changeIndices[i];
            lastPosition[idx] = i+1; //Timing is 1 based
        }

        for (int i=1; i <= n; i++) {
            if(lastPosition[i] == -1) {
                return false;
            }
        }

        map<int, int>positionIndexMap;
        for (int i=1; i<=n; i++) {
            int position = lastPosition[i];
            positionIndexMap[position] = i;
        }

        int used_till = 0;

        for (auto& [position, index] : positionIndexMap) {

            int req = 1 + nums[index-1];
            if (req + used_till > position) {
                return false;
            }

            used_till += req;
        }

        return true;
    }
    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {
        m = changeIndices.size();
        n = nums.size();

        int timeLeft = 0;
        int timeRight = m-1;
        int result = -1;

        while (timeLeft <= timeRight) {

            int midTime = timeLeft + (timeRight - timeLeft) / 2;

            if (isValid(midTime, changeIndices, nums)) {
                result = midTime + 1;
                timeRight = midTime - 1;
            }
            else {
                timeLeft = midTime + 1;
            }
        }

        return result;
    }
};
```
['C++']