Binary Search on Answer || C++ || brain teaser
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```\n\nclass Solution {\npublic:\n    bool check(int x,vector<int>&a ,vector<int> &b)\n{\n     long long count=0,index=0;\n     unordered_map<int,int> m;\n      unordered_map<int,int> d;\n     long long n=a.size();\n     for(int i=0;i<x;i++)\n     {\n       m[b[i]]=i;\n     }\n     \n     for(int i=0;i<x;i++)\n     {\n       if(i==m[b[i]])\n       {\n         if(count>=a[b[i]])\n         {\n           d[b[i]]++;\n           count-=a[b[i]];\n         }\n         else\n          return false;\n       }\n       else\n        count++;\n     }\n     return n==d.size();\n     \n     \n}\n    int earliestSecondToMarkIndices(vector<int>& a, vector<int>& b) {\n\n     int n=a.size();\n     int m=b.size();\n  \n    long long s=0;\n    for(auto&x:a)\n    {\n    \n     s+=x;\n    }\n    cout<<"s is "<<s<<endl;\n    \n    cout<<"n is "<<n<<endl;\n    cout<<"m is "<<m<<endl;\n    for(auto &x:b)\n    {\n   //  cin>>x;\n     x--;\n    }\n     unordered_map<int,int> mp;\n    for(int i=0;i<m;i++)\n    {\n      mp[b[i]]=i;\n    }\n    if(n!=mp.size())\n    {\n      //cout<<"here"<<endl;\n      return -1;\n    }\n    if(s+n>m)\n     {\n         cout<<"here"<<endl;\n      return -1;\n     }\n   \n \n   \n    long long l=s+n,h=m;\n    long long ans=m+1;\n    cout<<" l is "<<l<<endl;\n    cout<<" h is "<<h<<endl;\n    while(l<=h)\n    {\n       long long mid=l+(h-l)/2;\n       if(check(mid,a,b))\n       {\n         ans=min(ans,mid);\n         h=mid-1;\n       }\n       else\n        l=mid+1;\n      \n    }\n    if(ans==m+1)\n     ans=-1;\n    \n   \n    return ans;\n    \n   \n        \n       \n        \n    }\n};\n```
['Hash Table', 'Binary Search', 'C++']