Simple JS solution
Explanation\n\n[This code and explanation is not very clear, I have posted another solution here - https://leetcode.com/problems/earliest-second-to-mark-indices
# Explanation\n\n[This code and explanation is not very clear, I have posted another solution here - https://leetcode.com/problems/earliest-second-to-mark-indices-i/solutions/4784007/binary-search-simple-js-solution-commented-code]\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Think of minimum space required to acieve the solution. For nums = [2,2,0], we need sum of all numbers in nums (we need to decreased by 1 and get to 0) + length of nums (we need to mark each index of nums), a total of 7.\n\n2. Take the minimum space required (@minIndicesRequired), and call @findSolution function by passing the changeIndices subarray equal to minimum space required. If the function returns false, add 1 more indice from changeIndices to this subarray and repeat the process until we get a true OR we reach the end of changeIndices.\n\n\n3. In the findSolution function, find the last index for each position of nums and store it in @last Array. Now, iterate through each value of indicesArr and whenever you reach lastIndex of the current number, see if we can mark it. If yes, mark it and decreament available by its value. Available is basically the number of times we can decreament, which is everytime we are not at some last index.\n\n[I know my explanation is not very clear, I will update it in a couple of days]\n\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @param {number[]} changeIndices\n * @return {number}\n */\nvar earliestSecondToMarkIndices = function(nums, changeIndices) {\n    const len = nums.length;\n    const indicesLen = changeIndices.length;\n    let decCount = 0;\n    let reqPos = new Set();\n    for (i in nums) {\n        decCount += nums[i];\n        reqPos.add(i+1);\n    }\n \n    let minIndicesRequired = decCount + reqPos.size;\n    while (minIndicesRequired <= indicesLen) {\n        if (findSolution(changeIndices.slice(0,minIndicesRequired)) === true) {\n            return minIndicesRequired;\n        }\n        minIndicesRequired += 1;\n    }\n\n    function findSolution(indicesArr) {\n        let last = Array(nums.length).fill(-1); \n        for (let i = 0; i<indicesArr.length; i++) {\n            last[indicesArr[i] - 1] = i; // Indexed 0\n        }\n\n        // Couldn\'t find all last positions in indicesArr\n        for (value of indicesArr) { \n            if (value === -1) return false;\n        }\n\n        let available = 0;\n        let mark = Array(nums.length).fill(false);\n\n        for (let i = 0; i<indicesArr.length; i++) {\n            if (last[indicesArr[i] - 1] === i) {\n                if (nums[indicesArr[i] - 1] <= available) {\n                    mark[indicesArr[i] - 1] = true;\n                    available -= nums[indicesArr[i] - 1];\n                }\n            } else {\n                available += 1;\n            }\n        }\n\n        let markCount = 0;\n        for (value of mark) {\n            if (value === true) markCount += 1;\n        }\n\n        return markCount === len;\n    }\n\n    return -1;\n\n    \n};\n```
['JavaScript']