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.
It's basically just following the rules as written.
As you check, you find intervals that start with an odd value, have even values in between, and end in an odd value. The process is symmetrical, so for any unhandled interval it doesn't matter whether you start at the leftward odd number or the rightward one, and you will never have to reverse course. And it can only be unbalanced when there is an interval that doesn't get balanced when it reaches the end.
You disturb two values each time you have to give out any loaves. So if an odd number is followed by an even one, adding loaves makes an even number followed by an odd one, which shortens the interval. When the odd value slides far enough to be next to another odd one, adding loaves closes the interval and you just look for the next one.
If you reach the end without an open interval, you're done, but if you're still handing out carried loaves, the solution can't be found.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Rations
You are viewing a single comment's thread. Return to all comments →
It's basically just following the rules as written.
As you check, you find intervals that start with an odd value, have even values in between, and end in an odd value. The process is symmetrical, so for any unhandled interval it doesn't matter whether you start at the leftward odd number or the rightward one, and you will never have to reverse course. And it can only be unbalanced when there is an interval that doesn't get balanced when it reaches the end.
You disturb two values each time you have to give out any loaves. So if an odd number is followed by an even one, adding loaves makes an even number followed by an odd one, which shortens the interval. When the odd value slides far enough to be next to another odd one, adding loaves closes the interval and you just look for the next one.
If you reach the end without an open interval, you're done, but if you're still handing out carried loaves, the solution can't be found.