Isn’t the if conditions useless?


#1

Even if we get away with both if conditions we still will get same results. Right ?
Those if’s are there to confuse us to think that this is like binary search. Right ?


#2

right. the if cases are never satisfied , the only thing that runs is return statement


#3

if cases are doing the comparison. if we remove it then 1 will be added in all call (last statement of the code). So

  1. if you remove these if statements (including corresponding return statement) then the result should be size of the array always. As there will be no check of equality in this case.
  2. if you remove only if cases then array jump beyond its range in various scenario.

In fact all the logic is there in these two if statements and 1 is added only if k is equal to V[mid].


#4

both IF,s are there for right purpose as if we see the best case then we consider the array to be sorted and distinct in that case array will be divided in two parts and only one part will be checked and in that case answer will be equal to binary search.