For this problem, it’s easier to find out the lower and upper bound of $y$ as $y_{min} \implies x_{max}$ and $y_{max} \implies x_{min}$.
We rewrite the given equation as follows:
\begin{eqnarray} \ddfrac{1}{x} & = &\ddfrac{1}{k} - \ddfrac{1}{y} \nonumber \newline \label{uva-10976-eqn1} \implies \ddfrac{1}{x} & = & \ddfrac{ky}{y - k} \end{eqnarray}
Now what is $y_{min}$? We know both $x$ and $y$ are positive integers. So the denominator has to be positive. It can’t be equal to zero because otherwise a divide by zero will occur. We have,
\begin{aligned} y_{min} - k & > 0 \newline \implies y_{min} & > k \end{aligned}
What is the smallest positive integer that’s greater than $k$? It’s $k + 1$. So we have, $y_{min} = k + 1$.
To find $y_{max}$, we rewrite the given equation as follows:
\begin{eqnarray} \ddfrac{xy}{x + y} & = & k \nonumber \newline \label{uva-10976-eqn2} \implies xy & = & k(x + y) \end{eqnarray}
As $x$ and $y$ are both positive integers, we can employ AM-GM Inequality. From AM-GM, we know that,
\begin{eqnarray} \ddfrac{x + y}{2} & \geq & \sqrt{xy} \nonumber \newline \label{uva-10976-eqn3} \implies (x + y)^2 & \geq & 4xy \end{eqnarray}
Replacing \eqref{uva-10976-eqn2} in \eqref{uva-10976-eqn3} we get,
\begin{eqnarray} (x + y)^2 & \geq & 4k(x+ y) \nonumber \newline \label{uva-10976-eqn4} \implies (x + y) & \geq & 4k \end{eqnarray}
As $(x + y)$ is positive, we can divide the inequality safely. We’re also given another condition $x \geq y$. When $x = y$, we have $x_{min} = y_{max}$. Also, the equality in \eqref{uva-10976-eqn4} holds if and only if $x = y$. See this answer for a proof.
From \eqref{uva-10976-eqn4}, we then have,
\begin{eqnarray} 2 * y_{max} & = & 4k \nonumber \newline \implies y_{max} & = & 2k \nonumber \end{eqnarray}
Now that we’ve established a bound for the values of $y$, we can just calculate $\ddfrac{1}{x}$ from \eqref{uva-10976-eqn1}.
Here’s the implementation in C++
:
Time complexity: The for
loop runs $2k - (k + 1) + 1 = k $ times. So the time complexity is $\mathcal{O}(k)$.
Sharing is caring. Share this story in...
Share: Twitter Facebook LinkedIn