0
$\begingroup$

So I've an expression max(k-1, n-k)

Now, the book I'm reading is Introduction to Algorithms and the authors have deduced the following cases.

Screenshot from CLRS

I've no idea about how to approach this problem. I tried looking at the function to calculate max of two functions, but from that too, the problem seem to be unapproachable.

Is the solution purely based on intuition? Or there is any mathematical approach one can always rely upon, to solve such problems?

Sorry for the naive question.

  • 2
    $\max(a,b)=a$ if $a \geq b$ and it is $b$ otherwise. So $\max(k-1, n-k)=k-1$ if $k-1 \geq n-k$ and so on.2017-01-11

1 Answers 1

1

Though the OP hasn't mentioned, but looking at the equation it seems that $k$ and $n$ are integers.

$\displaystyle \begin{align} \max(k-1,n-k)=k-1\ \ \ &if\ \ \ k-1\ge n-k\\ &\Rightarrow 2k\ge n+1 \\ &\Rightarrow k\ge\frac{n+1}{2} \\ &\Rightarrow k\ge\lceil n/2\rceil \end{align}$

Similarly

$\displaystyle \begin{align} \max(k-1,n-k)=n-k\ \ \ &if\ \ \ k-1\le n-k\\ &\Rightarrow k\le\lceil n/2\rceil \end{align}$


Edit: Given $k\ge\frac{n+1}{2}$

Case(i):

If $n$ is odd, it is obvious that $\frac{n+1}{2}=\lceil n/2\rceil$.

Case(ii):

If $n$ is even, $\frac{n+1}{2}$ is not an integer, equality is not possible.

$k \ge \frac{n+1}{2} \Rightarrow k > \frac{n+1}{2}$

Since $\lceil n/2 \rceil - \frac{n+1}{2} < 1$

$\Rightarrow \lceil n/2 \rceil -1 <\frac{n+1}{2}$

$\Rightarrow k > \lceil n/2 \rceil -1$

$\Rightarrow k \ge \lceil n/2 \rceil$

Similarly do for $k\le\frac{n+1}{2}$

  • 0
    Where I was stuck is, how $\frac{n+1}{2}$ is $\lceil n/2 \rceil $2017-01-11
  • 1
    @RajatSaxena Added the explanation.2017-01-11