earliestSecondToMarkIndices in java
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\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        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```
['Java']