check out the analysis its already mention there that dividing by 2 is not optimal
For this test case, we cannot perform such direct splits because repeatedly splitting the maximum difference into halves is not optimal. For example, given a sequence [2, 12] and K = 2, splitting into halves will result in [2, 12] → [2, 7, 12] → [2, 7, 9, 12]. This way, the difficulty would be 5. However, if we perform [2, 12] → [2, 5, 12] → [2, 5, 8, 12], the difficulty would be 4.
first set of test cases is only for k=1 which means u can only input one value which works for your algo but it didnt work when k>1 look at this example:
For this test case, we cannot perform such direct splits because repeatedly splitting the maximum difference into halves is not optimal. For example, given a sequence [2, 12] and K = 2, splitting into halves will result in [2, 12] → [2, 7, 12] → [2, 7, 9, 12]. This way, the difficulty would be 5. However, if we perform [2, 12] → [2, 5, 12] → [2, 5, 8, 12], the difficulty would be 4.
What if I gave you the array and number D and asked to check if you can place K workouts anywhere you want and bring the max difference down to D or less ?
The same approach is being applied, we are searching for the lowest possible D by using binary search.
Suppose Mid= (L+R)/2 ,
now if we can get a max difference time of Mid or less ,then we search in the left half (R=Mid-1) looking for a lower value of D ,
If we can’t get a max difference time of Mid or Less, then obviously the answer is larger than Mid, so we search in the right half (L=MId+1) .