Changing Summation Limits: A Computational Solution To A Programming Interview Question

The Interview Question

We start our first turn by giving 1 item to the first person, 2 items to the second person, and so on until we give n items to the last person.
Then, we go back to the start of the row and begin our second turn, giving n+1 items to the first person, n+2 items to the second person, and so on until we give 2n items to the last person.
This process repeats (with us giving more items each time, and moving to the start of the row after we reach the end) until we cannot give the adequate amount of items to every person for a given turn.
Return an array that contains the number of items that are distributed for each turn in O(turn). (This means that it will only take twice as long to give out items for a row of 20 people than a row of 10 people even though there are around 4 times as many items to give out)

The Solution

If you would like to learn more about the proof behind this solution, check out this link here: https://brilliant.org/wiki/sum-of-n-n2-or-n3/

The Code

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store