Video Explanation (How to approach?)
Explanation\n\nClick here for the video\n\n# Code\n\n#define pii pair<int, int>\n#define F first\n#define S second\n\nclass Solution {\n    \n    int n;\n    ve
# Explanation\n\n[Click here for the video](https://youtu.be/TV0eqjommD0)\n\n# Code\n```\n#define pii pair<int, int>\n#define F first\n#define S second\n\nclass Solution {\n    \n    int n;\n    vector<int> arr;\n    vector<int> ci;\n    \n    bool IsValid (int till) {\n        vector<int> pos(n+1, -1);\n        for (int j = 0; j <= till; j ++) pos[ci[j]] = j;\n        \n        for (int j = 1; j <= n; j ++)\n            if (pos[j] == -1) return false;\n        \n        vector<pii> pos_and_ind;\n        for (int j = 1; j <= n; j ++) pos_and_ind.push_back({pos[j], j});\n        sort (pos_and_ind.begin(), pos_and_ind.end());\n                \n        int used_till = 0;\n        for (auto [pos, ind] : pos_and_ind) {\n            int req = 1 + arr[ind-1];\n            if (used_till + req > pos+1) return false;\n            used_till += req;\n        }\n        return true;\n    }\n    \npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        n = nums.size();\n        ci = changeIndices;\n        arr = nums;\n        \n        for (int t = 0; t < ci.size(); t ++) \n            if (IsValid(t)) return t+1;\n        \n        return -1;\n    }\n};\n```
['C++']