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.
This is not actually optimal, because you are still performing it in O(2n). If you instead nest your bread distribution iteration within your array intialization you can reach O(n) time and in that case there is no benefit to knowing the sum, because by the time you gain knowledge of the sum of the array you are already at the last element and can determine it without the sum. Therefore in the same space using a integer as a carry variable instead of a sum variable you are able to achieve this in the same space complexity proposed here, but with half the time required.
Best of luck!
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 →
This is not actually optimal, because you are still performing it in O(2n). If you instead nest your bread distribution iteration within your array intialization you can reach O(n) time and in that case there is no benefit to knowing the sum, because by the time you gain knowledge of the sum of the array you are already at the last element and can determine it without the sum. Therefore in the same space using a integer as a carry variable instead of a sum variable you are able to achieve this in the same space complexity proposed here, but with half the time required.
Best of luck!