How to approach problems like this

41 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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}$