I implemented my solution using Binary Search in this Problem A: Deadline. Issue is I'm Facing that,by changing the value of high from 1e18 to n-1, my solution which was giving wrong answer now being accepted. To be on the safe side, I kept high=1e18, but it's giving wrong answer. Any Help on this would be really appreaciated. Am I missing something conceptually on knowledge of Binary Search?
Wrong Answer due to high=1e18
Accepted after high=n-1
try high=1e18+2 and see if that still gives you WA
I believe both of your binary searches is wrong, consider the test case
1 5 9
, the answer should beYes
.(two days of optimization). However, your program givesNo
. This is because when in your binary searchlow = 1
,high = 2
, thenlow + (high - low) / 2 = 1
. You skip the possibility of using two days for optimization. For thehigh = n - 1
solution, it has the same problem but I believe it is coincidently correct under the settings of this particular problem. (or it is incorrect I am not sure)I agree, the function
x+(x+d)/(x+1)
is not monotonic so binary search should not be expected to work.okay thanks, but any other way to solve the problem using binary search as the problem has tag of binary search.
The function
f(x) = x+(x+d)/(x+1)
is concave upwards. It means that there are two intervals, for the first intervalf(x) > f(x + 1)
, and for the second intervalf(x) < f(x + 1)
. So you may binary search for the first x that satisfyf(x) < f(x + 1)