Explained like I would to 5 year old | Binary Search | Java | Beats 100.0%
Complexity\n- Time complexity:\nO(log m * n)\n\n- Space complexity:\nO(log m* n)\n\n# Code\n\nclass Solution {\n\n//Challo to bhai log, iss question mei, contes
# Complexity\n- Time complexity:\nO(log m * n)\n\n- Space complexity:\nO(log m* n)\n\n# Code\n```\nclass Solution {\n\n//Challo to bhai log, iss question mei, contest mei lag gyi. \n//But ye hai kaffi easy.\n//Binary search aata hai? Aata nhi hai to padhlo jakar.\n\n//Ab tum ye socho ki bhai answer ki range kya ho skti hai ?\n//Socho..\n//Are soch le, aage padhta jayega kya..\n//To range ho skta hai 1 second se lekar changeIndices ke length tkk.\n//Question mei bhi likha hai \'s\' is in order from \'1\' to \'m\'.\n\n//Ab range hogya hai [1,m]\n//Humare programming ki duniya mei countng 0 se start hoti hai to \n//range thoda sa badal dete hain\n//[0,m-1]\n\n// Range ki \'lower\' = 0 and \'upper\' = m-1.\n// Ab mannlo koi bhi number uthalo range mei, let say \'t\'.\n\n//Agar tum \'t\' time mei saare array ke elements ko mark kar diye.\n// to \'t+1\' mei kr hi doge? To \'t\' answer ho skta hai.\n// But bhai log ye socho ki \'t\' se kam mei bhi ho skta hai ho jaye, \n//to \'t\' ko answer banalo, but usse neeche bhi check kro, \n//mltb aur optimised answer dhundho tum [0,t-1] range mei.\n\n// Ab socho ki tum \'t\' time mei nahi kar pare saare elements ko mark, \n//to tumhe \'t\' se zyada time lagega na!\n// Isliye answer ko [t+1,m-1] mei dhundho ab.\n\n//Isko kehte hai "Binary Search on Answer".\n//Ye kaam isliye krra kyuki humara answer monotonic hai, \n//monotonic mtlb ya to increasing ya decreasing hai\n\n//Ab dikkat ki baat kya hai?? Kaise pta krein ki time \'t\'  pe possible \n//hai ya nahi !\n\n// Ek function banate hai \n// isPossible(int time, int[] nums, int[] changedIndices)\n// Ye bta dega.\n\n\n    public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {\n        \n        int ans = -1;\n\n        int l = 0; \n        int u = changeIndices.length-1;\n        while(l<=u) {\n            int m = l + (u-l)/2;\n            if(isPossible(m, nums, changeIndices)){\n                ans = m; \n                u = m-1;\n            } else {\n                l = m+1;\n            }\n        }\n\n        return ans==-1?-1:ans+1;\n    }\n\n//Mehnat krne ka time aagya sher !\n//Ye rha wo function jo jadu krega\n\n//Sbse pehle to ye socho ki.\n//ek index hai \'i\'\n// Ye to changeIndices mei kayi baar aayega.\n// But lets say 100 baar aaya, \n// tumhe iss index \'i\' ko mark kab krna chahiye?\n// obviously jab ye last baar aaye tb !\n// Kyuki baaki bache huwe 99 baar mei tum kuch aur skte ho, \n// kyuki ye to aage aayega hi !\n\n//Isliye ek array mei ye store krlo ki indices last baar kab aayenge\n\n//Ab challo process kro changeIndices ko from [0,time]\n//Kyuki itte hi time mei sab mark krna hai \n//isliye loop time tkk chalao\n\n// Ye socho ki ye jo index hai nums ka, agar wo aaage bhi aayega. \n// To tum,\n// iss time pe kisi ko decrement kr skte ho. \n// To ek variable mei store krlo\n// ki \'1\' operation hai tumhare pass of decrementing, \n// kisko kroge decrement, future ki baat h.\n\n//Ab ye dekho ki yr, agar current jo indx hai wo last baar hai, \n// aage nahi aayega\n\n// Tbto mark hi krna hai boss. Ab nhi kiya to gye, aage aayega nhi\n// Ab ye dekho ki mark krne ke liye kya hona chahiye value\n// Lets say ki value \'0\' hai tb to badhiya hai !\n// But value kuch \'x\' hai, \n// mtlb ki tumhe iss element ko \'x\' baar decrement krna tha \n// takki mark kr sko. \n\n// But kya itte \'operations\' the tumahre pass ?\n// Agar the to mark krdo, operations ghata do\n// Nahi the to tata bye bye, nahi possble hai boss.\n\n    private boolean isPossible(int time, int[] arr, int[] chngInd) {\n\n        int operations = 0;\n        int marked = 0;\n\n      \n//Ismie last baar kab aayega index process hora        \n        Integer[] last = new Integer[arr.length];      \n        for(int index = 0; index<=time; index++) {\n            last[chngInd[index]-1] = index;\n        }\n\n        for(int i = 0; i <=time ; i++) {\n            \n            int index = chngInd[i]-1;\n//Agar ye index last baar aara, to mark krne ka try krte hain.\n            if(last[index]!=null && last[index]==i) {\n\n                // Agar operation available hai, to mark krdo.\n                // Aur operations use krliye tmne to ghata do.\n                if(operations>=arr[index]){\n                    marked++;\n                    operations -= arr[index];\n                } \n                //nahi hai to Tata Bye Bye\n                else {\n                    return false;\n                }\n            } \n//Agar last baar nhi aara hai to operations badha lo decrement ke.         \n            else \n            {\n                operations++;\n            }\n\n        }\n\n//Agar tm mark kr paye saare elements to mazze wrna unsuccessful.\n        return marked == arr.length;\n    }\n}\n```
['Java']