Setup
The strength of an even number is determined by the amount of times we can successfully divide by 2 until we reach an odd number.
For example, if n = 12
, then:
12 / 2 = 6
6 / 2 = 3
We have divided 12 in half twice and reached 3, so the strength of 12 is 2
.
Given a closed interval [n, m], return the strongest even number in the interval. If multiple solutions exist, return the smallest of the possible solutions.
Examples
for [1, 2]
, return 2
. (1 has a strenght of 0; 2 has a strength of 1)
for [5, 10]
, return 8
.
Tests
[48, 56]
[129, 193]
Good luck~!
This challenge comes from alexmate on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (17)
In Go.
Ruby
Here's a Nim submission!
There's definitely room to improve this, but I'm just starting to look into Nim's standard library, and I don't know all that's available to me yet.
Did it long hand in js. Great puzzle!
Haskell
Try it.
Not the most efficient answer, but it is!
CodePen
Hello guys, this is my first time here, and this is my recursive approach:
EDIT without times:
Inefficient Python w/ tail recursion:
The optimization of using bit operations to find the "strength" of a number is a low hanging fruit, but the relationship between the number's strength and its binary representation can be taken much farther than that - to the point of solving this in logarithmic(!) time:
Explanation:
You can compare two numbers, padded to the same length, by finding their longest common prefix and then looking at the highest digit that's different. This work in any base, but farther ahead we'll use some properties unique to binary so let's stick with that.
For example, if we take 129 and 193 and convert them to binary:
We can see that both start with
000000001
, and then 129 has0000001
while 193 has1000001
. So the highest nonshared digit is0
for 129 and1
for 193 - which is why 193 is higher (yup - that's the direction the causation works here. Though this is math, so one can twist it to be the other way around...)Lemma: Any number between the lower and higher bounds will share that same prefix with them.
Proof: If it doesn't, then at least one digit is different. Take the most significant digit where the number is different than the shared prefix. If it's
0
for the number and1
for the prefix - this means the number is lower than the lower bound. If it's1
for the number and0
for the prefix - this means the number is higher than the higher bound.So, the strongest number, being in the range, must also have that prefix. The strongest number with that prefix is just the prefix with only zeroes after it (remember - the number of digits is fixed!). That's also the lowest number in that range, so if it's equal to the lower bound - that's the strongest number in the range. Otherwise - the next strongest number is the prefix, a single one after it, and after that only zeroes. This number is guaranteed to not be higher than the higher bound, because they share the original common prefix plus a single one after it - and that number is the lowest in that prefix, since after that it's all zeroes.
That is just so clean, well thought and well explained! Congrats!