We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
The test cases are alright, I think. Here's some Python code that works for your test cases too. It took me quite some time to crack this, but it was well worth it. Initially, I faced timeouts because I was subtracting the exponent instead of the largest power of 2 less than n.
Check this out:
def read_int():
return int(raw_input())
def is_power_of_2(n):
return n & (n - 1) == 0
def log2(n):
assert n > 0
exp = 0
while n:
n >>= 1
exp += 1
return exp
def largest_exp_of_2_less_than(n):
return log2(n) - 1
def steps(n):
i = 0
while n > 1:
if is_power_of_2(n):
n >>= 1
else:
n -= 1 << largest_exp_of_2_less_than(n)
i += 1
return i - 1
def is_even(n):
return not (n & 1)
def main():
t = read_int()
assert 0 < t < 11
for i in range(t):
print("Louise" if is_even(steps(read_int())) else "Richard")
if __name__ == '__main__':
main()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counter game
You are viewing a single comment's thread. Return to all comments →
The test cases are alright, I think. Here's some Python code that works for your test cases too. It took me quite some time to crack this, but it was well worth it. Initially, I faced timeouts because I was subtracting the exponent instead of the largest power of 2 less than n.
Check this out: