C++ Implementation using Binary Search using Hash Table set data structure
\n\n# Code\n\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        map<int,int>mp;\n        
\n\n# Code\n```\nclass Solution {\npublic:\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        map<int,int>mp;\n        for(auto x:changeIndices){\n            mp[x]++;\n        }\n        for(int i=0;i<nums.size();i++)\n        {\n            if(mp.count(i+1)==0){\n                return -1;\n            }\n        }\n        int mx=*max_element(nums.begin(),nums.end());\n        if(mx>changeIndices.size()){\n            return -1;\n        }\n        int sum=accumulate(nums.begin(),nums.end(),0);\n        int lo=0;\n        int hi=changeIndices.size()-1;\n        int ans=-1;\n        if(sum>changeIndices.size()){\n            return -1;\n        }\n        auto check=[&](int m){\n            \n            unordered_map<int,int>ind;\n            unordered_map<int,int>m1;\n            vector<int>tmp=nums;\n        set<pair<int,int>>mst;\n            \n            \n            for(int i=m;i>=0;i--)\n            {\n                if(m1.count(changeIndices[i]-1)==0){\n                    m1[changeIndices[i]-1]=i;\n                    if(tmp[changeIndices[i]-1]!=0)\n                        mst.insert({i,changeIndices[i]-1});\n                    ind[i]=changeIndices[i]-1;\n                }\n            }\n            if(m1.size()!=nums.size()){\n                return false;\n            }\n       \n            map<int,int>m2;\n            \n            // cout<<"m "<<m<<endl;\n            for(int i=0;i<=m;i++){\n                \n                // cout<<i<<endl;\n                // cout<<"ind.count(i) "<<ind.count(i)<<endl;\n                if(ind.count(i)){\n                    // cout<<ind[i]<<endl;\n                    if(tmp[ind[i]]!=0){\n                        // cout<<"Here"<<endl;\n                        // cout<<ind[i]<<" "<<tmp[ind[i]]<<" "<<i<<" "<<m<<endl;\n                        return false;\n                    }\n                    // cout<<"After ind[i] "<<i<<endl;\n                    m2[ind[i]]++;\n                    if(mst.count({i,ind[i]})){\n                        mst.erase(mst.find({i,ind[i]}));\n                    }\n                    // cout<<"After mst.erase()"<<endl;\n                    continue;\n                }\n                if(mst.empty()==true){\n                    // cout<<"Became Empty"<<endl;\n                    continue;\n                }\n                auto p=*mst.begin();\n                if(tmp[p.second]>0){\n                    tmp[p.second]--;\n                    if(tmp[p.second]==0){\n                        mst.erase(mst.find(p));\n                        \n                    }\n                }\n                else{\n                    if(tmp[p.second]==0){\n                        mst.erase(mst.find(p));\n                        \n                    }\n                }\n                \n            }\n            // cout<<m2.size()<<" "<<nums.size()<<endl;\n            return m2.size()==nums.size();\n        };\n        while(lo<=hi){\n            int m=(lo+hi)/2;\n            if(check(m)){\n                ans=m;\n                hi=m-1;\n            }\n            else{\n                lo=m+1;\n            }\n        }\n        return ans==-1?-1:ans+1;\n    }\n};\n```
['Hash Table', 'Binary Search', 'C++']