Contest 🤷‍♂️Me Index Mark Nhi Hui 🥲 || Hindi 💥 Explanation || C++ Solution
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n Dekho bhai akhir que hoga kaise\n Smart nhi bnta hu...kya keh rhe ho hu bhi nhi 
![image.png](https://assets.leetcode.com/users/images/68e0f28a-e1b7-40b3-8f02-8906c530835b_1708942015.0449538.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* Dekho bhai akhir que hoga kaise\n* Smart nhi bnta hu...kya keh rhe ho hu bhi nhi \uD83D\uDE02\n* Dheere dheere feel krte hai \n* aakhir Binary Search Kyu ? \n* Sidhe Sidhe sochate hai,\n* Dekho agr tum 5 second me pure index ko mark nhi kar pa rhe ho\n* to kya 4 sec me hoga, hmesha 5 ke baad hi hoga n\n* aur Que ke line me ek hint tha ki Earlist time batavo\n* Yani Jab sabse pehle mark karna possible ho\n* Uske baad to hoga hi ( kya keh rhe ho smjh aa gya )\n* Aage Badhu \uD83D\uDE0A\uD83D\uDE0A\uD83D\uDE0A\uD83D\uDE0A\n# Approach\n<!-- Describe your approach to solving the problem. -->\n* patience ke sath samjhna, code me \n* har line ke Why ke sath\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 {\npublic:\n    bool isPossible(vector<int>& nums,vector<int> &changeIndices,int time,int sum){\n        int n=nums.size(); \n\n        // dekho jitna time hai mere pass\n        // agr vo mere number of index ( nums ke )\n        // usse kam hai to knha se possible ho payega\n\n        // arey dekho 4 second yani 4 element \n        // aur nums me hai 10 ele kya nums ka har index mark ho payega\n        // nhi n simple ...\uD83D\uDE0A\n        if(time<n) return false;\n\n        // ye wala case explain kiya tha maine\n        // ki minimum time (sum + n) to hona hi chaiye\n        // hmare pass, samjhe\n        if(time<sum+nums.size()) return false;\n\n        // ye ye btayega ki n tak ki index mark hui ya nhi\n        vector<int> vis(n,0);\n\n        // ye count karega decrement\n        int cnt=0;\n        // ye count krega kitni index vis hui\n        // n tak hui ya nhi\n        int cnt2=0;\n\n\n        // dekho aakhir maine pichche se kyu traverse kiya\n        // kyuki pehle mujhe decremnet krna hai\n        // phir mark krna hia\n        // to har value yani 1 to n,  ki last\n        // index save krenge taki\n        // aage ke elemnts ka use \n        // decrement ke liye use kr sku\n\n        // test case => \n        // nums = 0 2 3 0\n        // chnage => 1 2 4 1 3 3 3 3 3 3 2 1\n\n        // agr aage se check kroge to 9 second me kaam hota dikhega \n        // jo ki galat hai,10 second lagega ye shi hai\n        for(int i=time-1;i>=0;i--){\n\n            // agr index visited nhi hai to visit kr do\n            if(!vis[changeIndices[i]-1])\n            {\n                vis[changeIndices[i]-1]=1;\n                // ye bta rha 1 to n me \n                // kitne index visit huye\n                cnt2+=1;\n\n                // us index ki value decrement krne ke liye\n                // itne opr ki jarurat to padegi hi\n                // yani value 0 to karni hi hai n\n                // tabhi mark kar payege\n\n                // thoda complex lag skta hai, doubt huaa hai to\n                // comment kr do\n                cnt+=(nums[changeIndices[i]-1]);\n            }\n            else\n            {\n                if(cnt>0)\n                {   // decrement kr rhe \n                    cnt-=1;\n                    if(cnt<0)cnt=0;\n                }\n            }\n            \n        }   \n\n        // agr decrement bhi ho gya and all visit then\n        // possible hai\n        if(cnt==0&&cnt2==n)return 1;\n        return false;\n    }\n    int earliestSecondToMarkIndices(vector<int>& nums, vector<int>& changeIndices) {\n\n        // ye to variables hai, esme kya btau\n        // tum hi bta do \uD83E\uDD72\n        int n=changeIndices.size();\n        int s=1;\n        int e=n;\n        int mid=0;\n\n        // kuchch nhi hoga to -1, to hoga hi \uD83D\uDE0A\n        int ans=-1;\n\n        // dekho minimum wala case sochate hai \n        // ki minimum kitna time chaiye hoga \n        // pure nums ko visit karne ke liye\n\n        // espe dhyan dena\n\n        // sum(nums ke har element ka) + nums ka size\n        // itna to krna hi padega\n\n        // kyu abhi bhai, aisa kyu\n        // dekho...\n        // 1 se decrement krenge yani utne number\n        // hone chaiye tabhi sabko decrement nikal payenge\n        // and mark karne ke liye n elements chaiye honge jo ki\n        // 1 to n index ko cover kar rhe ho\n        long long int sum=0; \n        for(auto it:nums) sum+=it;\n\n        // simple binary search template\n        while(s<=e){\n            mid=s+(e-s)/2;\n\n            // ye check krega, ki es mid time me \n            // possible hai ya nhi \u26A1\u26A1\u26A1\u26A1\n            if(isPossible(nums,changeIndices,mid,sum)){\n                ans=mid;\n\n                // earlist btana hai yani jis point pe\n                // ans mila uske pichche jake dekhenge possible \n                // hai ya nhi\n                e=mid-1;\n            }\n            else{\n                // agr us point pe possible nhi hai to\n                // aage dekhenge\n                s=mid+1;\n            }\n        }\n\n        // ans return kiya\n\n        // I hope har line ke Why ke sath, samjh aaya hoga\n        // doubt hai to comment kr do..\uD83D\uDE0A\u2764\uFE0F\n        return ans;\n    }\n};\n```
['Binary Search', 'C++']