- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

**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.

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.

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 + " "); } }

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

- Related Questions & Answers
- Minimum Number of Jumps Problem
- Minimum Number of Platforms Problem
- Java Program to find minimum sum of factors of a number
- Allocate minimum number of pages in C++
- Minimum Number of Refueling Stops in C++
- Minimum Number of Frogs Croaking in C++
- Maximum and minimum of an array using minimum number of comparisons in C
- Minimum number of Square Free Divisors in C++
- Minimum number using set bits of a given number in C++
- Find minimum sum of factors of number using C++.
- Find minimum element of HashSet in Java
- Minimum number of stops from given path in C++
- Minimum Number of Arrows to Burst Balloons in C++
- Minimum Number of K Consecutive Bit Flips in C++
- Minimum number of squares whose sum equals to given number n

Advertisements