C++ | binary search | easy understand | greedy | Beginner Friendly
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:\nO((n+m)*log(m))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int check(vector<int> &nums,vector<int> &index,int k)\n    {\n        unordered_map<int,int> m1,m2;\n        for(int i=0;i<=k;i++)\n        {\n            m1[index[i]]=i;\n        }\n        int count=0,ans=0;\n        for(int i=0;i<=k;i++)\n        {\n            count++;\n            if(m1[index[i]]==i)\n            {\n                if(count>=(nums[index[i]-1]+1))\n                {\n                    count=count-nums[index[i]-1]-1;\n                    ans++;\n                }\n                else\n                {\n                    return false;\n                }\n            }\n            \n        }\n        return ans==nums.size() ? true : false;\n    }\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& index) \n    {\n        int m=index.size()-1;\n        int i=0,j=m,mid,ans=INT_MAX,curr;\n        while(i<=j)\n        {\n            mid=i+(j-i)/2;\n            curr=check(nums,index,mid);\n            if(curr)\n            {\n                ans=mid;\n                j=mid-1;\n            }\n            else\n            {\n                i=mid+1;\n            }\n        }\n        return ans!=INT_MAX ? ans+1 : -1;\n    }\n};\n```
['C++']