BEATS 100% || BINARY SEARCH || BEGINNER FRIENDLY 🔥🔥
\n\n\nclass Solution {\npublic:\n\n    bool allMarked(vector<int>&marked){\n        for(int i=0;i<marked.size();i++){\n            if(!marked[i]) return false;\
\n\n```\nclass Solution {\npublic:\n\n    bool allMarked(vector<int>&marked){\n        for(int i=0;i<marked.size();i++){\n            if(!marked[i]) return false;\n        }\n        return true;\n    }\n    bool possible(int mid,vector<int>& nums, vector<int>& changeIndices){\n        long long count=0;\n        \n        vector<int>marked(nums.size(),0);\n        for(int sec=mid;sec>=0;sec--){\n            if(!marked[changeIndices[sec]-1]){\n                marked[changeIndices[sec]-1]=1;\n                count+=nums[changeIndices[sec]-1];\n            }\n            else if(marked[changeIndices[sec]-1]){\n                if(count>0) count--;\n            }\n        }\n       \n        if(allMarked(marked) && count==0) return true;\n        return false;\n    }\n\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n        long long low=0;\n        long long high=changeIndices.size()-1;\n        long long ans=INT_MAX;\n\n        while(low<=high){\n            \n            long long mid=(long long)low+(high-low)/2;\n            if(possible(mid,nums,changeIndices)){\n                ans=min(ans,(long long)mid);\n                high=mid-1;\n            }\n            else{\n                low=mid+1;\n            }\n        }\n        return ans==INT_MAX?-1:(int)ans+1;\n    }\n};\n```
['Array', 'Binary Search', 'C++']