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.
Hi! I came up with same solution. If someone is having a hard time to understand it, here's some examples to help:
First, notice that we need an even amount of odd numbers. Because the problem says that if we increase i by one we need to increase i+1 or i-1. So we need a even amount of odd numbers to get as result an array with only even numbers.
Example: B = 2 3 5 6 this array has two odd numbers. To make 3 became even we add 1 to it. Luckly, it has an neighbor with odd number. So we add to it too, like this B = 2 4 6 6 . Now the array has only even number.
But if there's even numbers between the the odd numbers? It will require more loaves.
Example: B = 2 3 4 5 6 this array has two odd numbers. To make 3 became even we add 1 to and it's neighbor, like this B = 2 4 5 5 6 . Now the odd number was pushed do the left, so we need to increse it and its neighbor too, like this B = 2 4 6 6 6 . Finally the array has only even number.
Example: B = 2 3 4 4 4 5 6 this array has two odd numbers. To make 3 became even we add 1 to and it's neighbor, like this B = 2 4 5 4 4 5 6 . Now the odd number was pushed do the left, so we need to increse it and its neighbor too, like this B = 2 4 6 5 4 5 6 . If we continue with this process we will end up with B = 2 4 6 6 6 6 6
Now notice that on the last two examples the amount of loaf increased by one 1 were it was odd and by two on the numbers between the odd ones. With that in mind, we can calculate the amout of loaves
And we repeat this for every two odd numbers in the original array;
Example: B = 3 5 3 4 4 5 this array has four odd numbers on indexes 0, 1, 2, 5. So we calculate on indexes 0 and 1 which results in 2. And we calculate on indexes 2 and 5, which results in 1 + 2.2 + 1 = 6. And 2 + 6 = 8 is the result.
Fair Rations
You are viewing a single comment's thread. Return to all comments →
Hi! I came up with same solution. If someone is having a hard time to understand it, here's some examples to help:
First, notice that we need an even amount of odd numbers. Because the problem says that if we increase i by one we need to increase i+1 or i-1. So we need a even amount of odd numbers to get as result an array with only even numbers.
But if there's even numbers between the the odd numbers? It will require more loaves.
Now notice that on the last two examples the amount of loaf increased by one 1 were it was odd and by two on the numbers between the odd ones. With that in mind, we can calculate the amout of loaves
amountLoaves = 1 + 2.evenNumbersBetweenOddOnes + 1;
And we repeat this for every two odd numbers in the original array;
Here's my code: https://www.hackerrank.com/challenges/fair-rations/submissions/code/130803395