An easy brute force problem. As the input will be from the set $\{2,4,6,8\}$, we can precalculate the answer.
You have to iterate $10^2, 10^4, 10^6$ and $10^8$ numbers for powers of $2,4,6$ and $8$ respectively. One possible option could be:
Iterate the numbers as string. Increment string by $1$ at the end of each iteration.
In each iteration, you divide the current string $s$ into two substrings. (The string length is even). One from index $\big[0, \ddfrac{s.\textrm{length}}{2}\big)$ and another from $\big[\ddfrac{s.\textrm{length}}{2}, s.\textrm{length}\big)$. Convert the two halves into integers. Then check the given constraint.
This comes to mind first and is easier to implement. But unfortunately it will timeout. Why? Take the of precomputing number of $8$ digits. We’re already iterating over $10^8$ elements. The above approach requires $8 + 8 = 16$ operations per element ($8$ for substring conversion and another $8$ for converting to integer. It could be more than that. For example when we need to increment all the index values. But we’re disregarding that.) So, it results in total $10^8 * 16$ operations, which requires $16$ seconds to run (assuming $10^8$ operations per second), but the time limit is $3$ seconds!
So what can we do instead of string operations? We’ll we only have only one alternative left. That’s to iterate the numbers as integers. But how then we’ll divide the numbers into two halves? We can use the following approach:
To divide a number $n$ in base $b$ into two halves, the first half comprising $x$ digits and the second half comprising $y$ digits ($ x + y = n$), we divide the number by $b^y$. The quotient of this division is the first half and the remainder is the second half.
For example, take $n = (49736)_{10}$, a number in base $10$. To divide $n$ into two halves, the first one containing $2$ digits and the second one containing $3$ digits, we divide $n$ by $10^3$. The quotient is $49$ and the remainder is $736$. So there you go, the number is split into two.
Now for our problem we need to divide the number into two halves of equal digits. So for $10$’s power of $n$ ($n = \{2,4,6,8\}$), we need to divide the $10^n$ by $10^{\sfrac{n}{2}}$ and take the quotient and remainder. Finally we need to check the constraint.
Why is this approach faster? Because we’re not looping over anything. And also arithmetic operations are much faster than iterating over strings because there’s dedicated registers in the hardware for the former.
Here’s the implementation in C++
:
Sidenote: C++
has no built-in library function for calculating power of integers. You can use pow()
but it returns double
, so you may run into precision error. Silicon Graphics has made its implementation of the C++ Standard Template Library freely available to the public. In the GNU library, it’s available as an SGI extension __gnu_cxx::power
in <ext/numeric>
. The signature of the function is power(TYPE Tp, Integer a)
and computers the power in $\mathcal{O}(\log{a})$. Find out more about it in here.
Sharing is caring. Share this story in...
Share: Twitter Facebook LinkedIn