Why we are taking only maximum value of b from i+1 to n?? It may be possible that value of b comes out to be very large like 1e9 then answer will not be minimum
Hey Prashu ! Here we are trying out every possibility. So, at any point of time, we are fixing 'a' then finding the best possible 'b' for the selected 'a'. Now if you fix 'a', then whatever values are there ahead of it, we have to select them as 'b' only as the array is sorted in ascending order and we need to check for the most expensive gift only that's why we are taking the maximum. so, if the maximum is very large then the answer will not come corresponding to that as we are updating the answer at every step.
bro i submitted this question but i am getting TLE can you make me where to change from bisect import bisect_left as binL from bisect import bisect_right as binR def ss(sortb,d): sortb = list(sortb) if len(sortb)==0: return -1 inx = binR(sortb,d) n = len(sortb) x,y = float('inf'), float('inf') if inx0: y = sortb[inx-1] if abs(x-d)
the way you explain this problem is mind blowing Thank you so much
Thank you so much for the appreciation.
wonderful explanation..keep up the good work
Really liked your explanation.Mind blowing! Thank you so much!
If you get WA on test 69, handle the edge case of i == n-1, separately
Nice explanation👋👋
super explanation! keep the good work going.
Thanks a lot!
Why we are taking only maximum value of b from i+1 to n??
It may be possible that value of b comes out to be very large like 1e9 then answer will not be minimum
Hey Prashu ! Here we are trying out every possibility. So, at any point of time, we are fixing 'a' then finding the best possible 'b' for the selected 'a'. Now if you fix 'a', then whatever values are there ahead of it, we have to select them as 'b' only as the array is sorted in ascending order and we need to check for the most expensive gift only that's why we are taking the maximum. so, if the maximum is very large then the answer will not come corresponding to that as we are updating the answer at every step.
1600 qns are so hard
Don't worry! Practice until you find them easy.
Can you share your CF handle ?
bro i submitted this question but i am getting TLE
can you make me where to change
from bisect import bisect_left as binL
from bisect import bisect_right as binR
def ss(sortb,d):
sortb = list(sortb)
if len(sortb)==0:
return -1
inx = binR(sortb,d)
n = len(sortb)
x,y = float('inf'), float('inf')
if inx0:
y = sortb[inx-1]
if abs(x-d)