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 concept is simple: Each total is of this form:
total = i*a + j*b
where
i + j == n (number of stones)
If we switch a stone from one term to the other, the total changes by the difference between a and b. So, successive totals always differ by abs(a-b). Finding the lowest and highest is trivial (it's where i or j is zero).
I had to rethink when my recursive solution timed out. I should have guessed that would happen based on the input range.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Manasa and Stones
You are viewing a single comment's thread. Return to all comments →
The concept is simple: Each total is of this form:
where
If we switch a stone from one term to the other, the total changes by the difference between a and b. So, successive totals always differ by abs(a-b). Finding the lowest and highest is trivial (it's where i or j is zero).
I had to rethink when my recursive solution timed out. I should have guessed that would happen based on the input range.