Java || Binary Search || beats 100% time complexity
Intuition\n Describe your first thoughts on how to solve this problem. \n1. Find if at second k is it possible to mark all the indexes\n\n# Approach\n Describe 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Find if at second `k` is it possible to mark all the indexes\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Lets reduce the problem as is it possible to mark all the indices after `k` seconds. And after that we need to fing the smallest second when all the indices be marked\n2. The smallest second can be found using binary search as once all the indices are marked we dont need to do any operation. F(k) is monotonic: k being seconds and value being if it is possible to mark all the indices after k seconds.\n\nApproch to solve the problem mentioned in point 1 is as follows\n\n1. For an index `j`, At the last appearance in the `changeIndices`, we need minimum of `nums[j]` decrement operations available, and this second can be used to mark the index.\n2. And the non last index apprearances can be used for decrement operations.\n3. Condition 1 should hold for all the indices\n\n# Complexity\n- Time complexity: $$O(M*LogM)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(M)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        \n        int l = 0, r = changeIndices.length-1, m;\n        \n        int ans = -1;\n        \n        while (l <= r) {\n            m = l + (r-l)/2;\n            \n            if (isPossible(nums, changeIndices, m)) {\n                ans = m+1;\n                r = m-1;\n            } else {\n                l = m+1;\n            }\n        }\n        \n        return ans;\n    }\n    \n    public boolean isPossible(int[] nums, int[] inds, int k) {\n        \n        int n = nums.length;\n        \n        // check if all values from 1 -> n are present in inds[0...k] ;\n        Set<Integer> conts = new HashSet<Integer>();\n        boolean[] isLast = new boolean[k+1];\n        \n        // prepare isLast\n        for(int i = k; i >= 0;i--) {\n            \n            if (conts.contains(inds[i])) continue;\n            \n            isLast[i] = true;\n            conts.add(inds[i]);\n        }\n        \n        if (conts.size() != n) return false;\n        \n        // verify\n        int needed = 0, available = 0;\n\n        for(int i = 0;i<=k ; i++) {\n            \n            if (!isLast[i]) {\n                available++;\n            } else {\n                \n                needed += nums[inds[i] - 1];\n            \n                if (needed > available) return false;\n            }\n        }\n        \n        return true;\n    }\n}\n```
['Java']