100% fastest JAVA solution using BinarySearch
Intuition\n Describe your first thoughts on how to solve this problem. \nSo it seems like a DP problem but its greedy So we can mark index in nums when number a
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSo it seems like a DP problem but its greedy So we can mark index in nums when number appears in changeIndices array so we find last occurance for each index and check if WE could make index zero before the last chance to mark it\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use binary search to get an Index if possible we check for earlier else we check for more time \n2. in **Check function** we will store last occurances of all indexes and greedily figure out if we can make index zero before its last occurance \n2.1 We first check if we have all indexes present to mark them if not \nwe return false\n2.2 variable moves store the spare indexes which we can use to make current indez zero. Its range between two final indexes (x,y)\n2.3 If for any case it becomes less than zero \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(log(S)) for binary search \nand O(S) in Check function \noverAll O(Slog(S))\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(S)\n# Code\n```\nclass Solution {\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        int sum = 0,n = nums.length,s = changeIndices.length;\n \n        for(int i : nums){\n           sum +=  i; \n           if(sum>s)return -1;\n        } \n \n        int l = sum, h = s-1,res = -2;\n\n        while(l<=h){\n           int m = (l+h)>>1; \n          \n           if(check(m,nums,changeIndices)){\n              res = m ;\n              h = m-1;\n           }else l = m+1;\n        }\n       \n        return res+1;\n    }\n\n    boolean check(int m,int nums[],int[] cidx){\n       var vis = new HashSet<Integer>(); \n       var list = new ArrayList<Integer>(); \n       \n      for(int i = m; i>=0; i--){\n         if(!vis.contains(cidx[i]))list.add(i);\n         vis.add(cidx[i]);\n      }\n        \n      if(vis.size() != nums.length)return false;\n      \n      int moves = 0; \n\n      for(int i = list.size()-1; i>=0; i--){\n         moves += list.get(i)-(i==list.size()-1?-1:list.get(i+1))-1;\n         moves -= nums[cidx[list.get(i)]-1]; \n         if(moves<0)return false;\n      }\n \n      return true;\n   }\n}\n\n\n```
['Binary Search', 'Java']