Welcome! Are you ready to embark on a captivating journey into the world of array manipulations? Today, we're going to explore a fascinating scenario involving a wonderful small town, its houses, and a fun balloon game. Without further ado, let's dive right in!
Picture a quaint, small town where every house is numbered sequentially from 1 to n
. One day, a festive town event is held, and balloons are tied to each house. The festivities do not end there. At the conclusion of the event, a fun game is played: at each step of the game, each house sends half of its balloons to the neighboring house simultaneously (the neighbor to the right side, and for the last house, the neighbor is the first house). The game goes on until, at some step, there are no changes in the number of balloons compared to the previous step.
The task is to create a C++ function, int solution(vector<int> balloons)
, where balloons
is a vector representing the number of balloons at each house. The function should simulate this game and return the number of steps in the game.
For example, if balloons = {4, 1, 2}
, the output should be solution({4, 1, 2}) = 3
. After the first step, the list becomes {3, 3, 1}
. This is because the first house sends 2 balloons and gets 1, the second house sends nothing but gets 2, and the third house sends 1 but receives nothing. Note that when the number of balloons x
is odd, then the house sends balloons. After the second step, the list becomes and never changes after that. So after the third step, the process finishes.
