Lesson 5

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). When counting the half of the balloons, we consider the existing balloons before receiving any balloons from a neighbor. 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 Java function, `int solution(int[] balloons)`

, where `balloons`

is an array 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(balloons) = 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 `(x - 1) / 2`

balloons. After the second step, the list becomes `{2, 3, 2}`

and never changes after that. So after the third step, the process finishes.

Firstly, it's essential to note that we're dealing with a **cyclical event**. In other words, when iterating over our `balloons`

array, we need to perceive the array as circular, meaning `balloons[n - 1]`

should refer back to `balloons[0]`

. This concept of cyclicity becomes crucial when we consider the last house passing balloons to the first.

Confident in our understanding of the problem, we move on to programming our solution. First, we need to set up a loop to iterate through the rounds of balloon sharing. This loop should continue as long as the array changes.

Java`1public class BalloonGame { 2 public static int solution(int[] balloons) { 3 int steps = 0; 4 while (true) { 5 steps += 1; 6 int[] newBalloons = balloons.clone(); // Store updated balloon counts 7 // TODO: Share the balloons 8 if (Arrays.equals(newBalloons, balloons)) { 9 break; 10 } 11 balloons = newBalloons; // Update balloons with new counts. 12 } 13 return steps; 14 } 15 16 public static void main(String[] args) { 17 int[] balloons = {4, 1, 2}; 18 System.out.println(solution(balloons)); // Output should be 3 19 } 20}`

Our next step delves into the core game mechanics: sharing the balloons. Throughout each cycle, each house must share half of its balloons with the next house.

We must also ensure that the last house shares balloons with the first house at the end of each cycle — for this, we'll use the modulo `%`

operator.

Here's the updated solution, complete with the mechanics of balloon sharing:

Java`1import java.util.Arrays; 2 3public class BalloonGame { 4 public static int solution(int[] balloons) { 5 int n = balloons.length; 6 int steps = 0; 7 while (true) { 8 steps += 1; 9 int[] newBalloons = balloons.clone(); // Store updated balloon counts 10 for (int i = 0; i < n; i++) { 11 int share = balloons[i] / 2; // Balloons to share 12 newBalloons[i] -= share; // Decrease balloons of current house 13 newBalloons[(i + 1) % n] += share; // Increase balloons of next house 14 } 15 if (Arrays.equals(newBalloons, balloons)) { 16 break; 17 } 18 balloons = newBalloons; // Update balloons with new counts. 19 } 20 return steps; 21 } 22 23 public static void main(String[] args) { 24 int[] balloons = {4, 1, 2}; 25 System.out.println(solution(balloons)); // Output should be 3 26 } 27}`

Congratulations on mastering this crucial programming scenario! You've successfully navigated a task involving the simulation of real-world events using array manipulation.

What's next? Now is the time to put into practice everything we've learned today. Try designing different versions of this balloon-sharing game. As always, happy coding!