Java | Binary Search
\nclass Solution {\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        int n = nums.length;\n        changeIndices = Arrays.
```\nclass Solution {\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        int n = nums.length;\n        changeIndices = Arrays.stream(changeIndices).map(i -> i-1).toArray();\n        \n        int l = 0, r = changeIndices.length - 1, res = 0;\n        while(l <= r) {\n            int mid = l + (r-l)/2;\n            if(feasible(nums, changeIndices, mid)) {\n                res = mid;\n                r = mid - 1;\n            } else {\n                l = mid + 1;\n            }\n        }\n        \n        return feasible(nums, changeIndices, res) ? res + 1 : -1;\n    }\n    \n    public boolean feasible(int[] nums, int[] changeIndices, int s) {\n        int n = nums.length;\n        Map<Integer, Integer> lastChance = new HashMap<>();\n        for(int i = 0; i <= s; i++) {\n            lastChance.put(changeIndices[i], i);\n        }\n        \n        int op = 0, marked = 0;\n        for(int i = 0; i <= s; i++) {\n            int currIndexWeCanChange = changeIndices[i];\n            if(lastChance.containsKey(currIndexWeCanChange) && lastChance.get(currIndexWeCanChange)== i) { // This is the last chance to change the currentIndexWeCanChange\n                // SINCE it\'s the last chance\n                // IF we can do less changes than needed return false\n                if(nums[currIndexWeCanChange] > op) \n                    return false;\n                op -= nums[currIndexWeCanChange];\n                marked++;\n            } else {\n                op++;\n            }\n        }\n        \n        return marked == n;\n    }\n}\n```
['Binary Tree', 'Java']