RunTime Error - ChefCode (may long challenge)

I’m getting Run time errors for some of the test cases, though the code is short, I’m unable to find the bug. Would anyone please look into it ?.
Thanks

link:


(https://www.codechef.com/viewsolution/14307998)

Your logic is correct, but your binary search is faulty. Try replacing with this code instead:

// In line 47, call: bin1(val,0,a2.size());

ll bin1(double val, ll str, ll ed) {
  if (str == ed) return str;
  int mid = (str + ed) / 2;
  if (a2[mid] <= val) return bin1(val, mid + 1, ed);
  else return bin1(val, str, mid);
}
1 Like

How did you figure it out ?? The code seemed a bit complex to me :confused:

Thanks, it worked. May I know what was wrong with my implementation?

This line:

if(ed<str)
    return -1;

and the last:

else
    return bin1(val,str,mid1-1);

What happens is if everything else is > val, your function returns -1, when it should return 0. There are some more special cases I forget to mention, but the idea is that the else block should at least contain mid1 in the range.