# Minimum number of bombs in Java

The problem statement here is to kill the goons in the rooms of a building with minimum number of bombings. The rooms are labelled as 1 to n. The goons are injured by the first bombing attack and die in the second. When the rooms are bombed the goons rush to the nearest room in the building specially the neighboring room. We must calculate the number of bombing it is required to bomb the rooms in order to kill all the goons in the building.

## Let us understand with example

Input − int number of rooms = 3

Output −Total Bombings required

4

2 1 3 2

Explanation − Here the minimum number of bombs required are 4.Let us consider we first bomb the 2nd room the goons will rush to 1st or 3rd room to save themselves. Then we bomb the first room, here some of the injured goons from the attacks of 2nd room will die and the goons who were hiding in the 1st room gets injured and rush to 2nd room as it is nearest to them. Now we bomb the 3rd room here the goons from 2nd room bombing were hiding so they get died in the bombing of 3rd room, and also the goons who were hiding in the 3rd room will rush to the nearest room i.e 2nd room at last we bomb the 2nd room here the injured goons from 3rd and 1st room gets killed and this completes our mission.

Input − int number of rooms = 2

Output −Total Bombings required

3

2 1 2

Explanation − Here the minimum number of bombs required are 3.Let us consider we first bomb the 2nd room the goons will rush to 1st room to save themselves. Then we bomb the first room, here some of the injured goons from the attacks of 2nd room will die and the goons who were hiding in the 1st room gets injured and rush to 2nd room as it is nearest to them. Now we bomb the 2nd room here. The goons from 1st room bombing who were hiding get killed and this completes our mission.

## Approach used in the below program is as follows −

• First the number of rooms is taken as input from the user.

• The number of bombs required is calculated as (n+n/2) and later printed.

• Then we bomb all the even rooms of the building and it is printed.

• After this we bomb the odd rooms of the building and we print them subsequently.

• Finally, we bomb the even rooms of the building again to end the bombing process and the result is printed to the user.

## Example

public class TP{
public static void main(String[] args){
int n = 8;
System.out.println("Total Bombings required");
System.out.println(n + n / 2);
for (int i = 2; i <= n; i += 2)
System.out.print(i + " ");
for (int i = 1; i <= n; i += 2)
System.out.print(i + " ");
for (int i = 2; i <= n; i += 2)
System.out.print(i + " ");
}
}

## Output

If we run the above code it will generate the following Output

Total Bombings required
12
2 4 6 8 1 3 5 7 2 4 6 8