You can avoid doing +1 for mid by doing this as everytime your ans will become 1 less than low:- bool check(long long time, long long n, vector batteries) { long long sum = 0; for (long long i : batteries) { sum += min(time, i); } return (sum >= (long long)time*n); } long long maxRunTime(int n, vector& batteries) { long long sum = 0; for (int i = 0; i < batteries.size(); i++) { sum += batteries[i]; } long long low = 1; long long high = sum / n; long long mid; while (low
@JeevanKumar-code Жыл бұрын
Upcoming Workshop on LinkedList: us06web.zoom.us/meeting/register/tZErf-ioqT0uGNPQbn4BMqWTx9s6EoOW7fcZ
@jr_jeet Жыл бұрын
Nice sir
@JeevanKumar-code Жыл бұрын
Thank you!
@leepakshiyadav1643 Жыл бұрын
amazing, explanation :)
@JeevanKumar-code Жыл бұрын
Thank you!
@ritikagrover2724 Жыл бұрын
class Solution { public boolean check(int n, int []B,long time){ long sum=0; for(int i:B){ sum+=Math.min(i,time); } return (sum>=time*n); } public long maxRunTime(int n, int[] batteries) { long sumPow=0; for(int i:batteries) sumPow+=i; long left=1, right=sumPow/n; while(left
@saudphysics5206 Жыл бұрын
what is pool and why it is used?
@prasidmitra6859 Жыл бұрын
The first approach is O(mlogm) m = no of batteries and the binary search approach is O(mlogk). It's given that k >> m. It seems that the frist one is better. Why use binary search?
@devverma3168 Жыл бұрын
why are we doing +1 for calculating mid
@JeevanKumar-code Жыл бұрын
Hey, since we are returning left as the ans and updating l = mid. Say l = 5 and r = 6 mid = 5 and leads to infinite loop
@geetdeveloper Жыл бұрын
if you do not add 1, it will always pick left mid in even length or array, but here we want to pick and try check for right mid element of array, therefore added +1