There are aliens in n buildings (minimum of 1 in each) and you have to kill all of them minimum number of bombings. Buildings are numbered as 1 – n. Aliens in a bombed building gets injured at the first bombing and die at the second. When a building is bombed for the first time, aliens in that building try to escape to the nearest building (for first building the nearest one is the second one and for nth building it is n-1). Calculate the minimum number of bombs required to kill all the aliens and the order of bombings.

Example:

Input: 3 Output: 4 2 1 3 2 Explanation: Minimum number of bombs required are 4. First bomb the 2nd building, aliens will move to 1st or 3rd to save themselves. Then bomb at 1st building, if some aliens have moved from 2nd building to 1st they will be killed and the 1st building aliens will be injured, and they will move to the 2nd building as it is nearest to them. Now, bomb at the 3rd building to kill aliens who moved from the 2nd building to 3rd and injure 3rd building aliens so they move to 2nd building as it is nearest to them. Now, bomb at the 2nd building again and all aliens who moved from 1st or 3rd building will be killed. Input: 2 Output: 3 2 1 2

We can make a constructive way to kill all aliens. Since everyone either moves to the left or to the right, we have to make sure that all the even positions are attacked, once at the start to injure aliens and the other time at the end. When we attack aliens at the even positions first time they move to the odd position buildings, so attack them at odd to kill all the previous even positions and injure the odd position aliens. The odd position aliens get injured and will move to the even position, so attack them at the even at the end to kill them.

The number of ways will be *n/2 + n/2 + n/2* which is **n + n/2. **

Below is the implementation of the above approach:

## C++

`// CPP program to find number of bombings required ` `// to kill all aliens. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to print where to shoot ` `void` `print(` `int` `n) ` `{ ` ` ` `// no. of bombs required ` ` ` `cout << n + n / 2 << endl; ` ` ` ` ` `// bomb all the even positions ` ` ` `for` `(` `int` `i = 2; i <= n; i += 2) ` ` ` `cout << i << ` `" "` `; ` ` ` ` ` `// bomb all the odd positions ` ` ` `for` `(` `int` `i = 1; i <= n; i += 2) ` ` ` `cout << i << ` `" "` `; ` ` ` ` ` `// bomb all the even positions again ` ` ` `for` `(` `int` `i = 2; i <= n; i += 2) ` ` ` `cout << i << ` `" "` `; ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `print(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number of bombings ` `// required to kill all aliens. ` `class` `GFG { ` ` ` ` ` `// function to print where to shoot ` ` ` `static` `void` `print(` `int` `n) ` ` ` `{ ` ` ` ` ` `// no. of bombs required ` ` ` `System.out.println(n + n / ` `2` `); ` ` ` ` ` `// bomb all the even positions ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i += ` `2` `) ` ` ` `System.out.print( i + ` `" "` `); ` ` ` ` ` `// bomb all the odd positions ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i += ` `2` `) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` ` ` `// bomb all the even positions again ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i += ` `2` `) ` ` ` `System.out.print( i + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `print(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`"""Python program to find number of ` `bombings required to kill all aliens"""` ` ` `# function to print where to shoot ` `def` `bomb_required(n): ` ` ` ` ` `# no. of bombs required ` ` ` `print` `(n` `+` `n ` `/` `/` `2` `) ` ` ` ` ` `# bomb all the even positions ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `, ` `2` `): ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` ` ` `# bomb all the odd positions ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `, ` `2` `): ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` ` ` `# bomb all the even positions again ` ` ` `for` `i ` `in` `range` `(` `2` `, n, ` `2` `): ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` `# Driver Code ` `bomb_required(` `3` `) ` ` ` `# This code is contributed by Abhishek Agrawal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of bombings ` `// required to kill all aliens. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to print where to shoot ` ` ` `static` `void` `print(` `int` `n) ` ` ` `{ ` ` ` ` ` `// no. of bombs required ` ` ` `Console.WriteLine(n + n / 2); ` ` ` ` ` `// bomb all the even positions ` ` ` `for` `(` `int` `i = 2; i <= n; i += 2) ` ` ` `Console.Write( i + ` `" "` `); ` ` ` ` ` `// bomb all the odd positions ` ` ` `for` `(` `int` `i = 1; i <= n; i += 2) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` ` ` `// bomb all the even positions ` ` ` `// again ` ` ` `for` `(` `int` `i = 2; i <= n; i += 2) ` ` ` `Console.Write( i + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` `print(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find number ` `// of bombings required to ` `// kill all aliens. ` ` ` ` ` `// function to print ` `// where to shoot ` `function` `p_rint(` `$n` `) ` `{ ` ` ` ` ` `// no. of bombs required ` ` ` `echo` `floor` `(` `$n` `+ ` `$n` `/ 2),` `"\n"` `; ` ` ` ` ` `// bomb all the even positions ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `+= 2) ` ` ` `echo` `$i` `,` `" "` `; ` ` ` ` ` `// bomb all the odd positions ` ` ` `for` `( ` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `+= 2) ` ` ` `echo` `$i` `, ` `" "` `; ` ` ` ` ` `// bomb all the even positions again ` ` ` `for` `( ` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `+= 2) ` ` ` `echo` `$i` `, ` `" "` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 3; ` ` ` `p_rint(` `$n` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

4 2 1 3 2

**Time complexity:** O(n)

This article is contributed by **Raj**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find minimum number to be divided to make a number a perfect square
- Convert a number m to n using minimum number of given operations
- Minimum number of given powers of 2 required to represent a number
- Smallest number dividing minimum number of elements in the array | Set 2
- Smallest number dividing minimum number of elements in the Array
- Minimum divisor of a number to make the number perfect cube
- Minimum number of squares whose sum equals to given number N | set 2
- Minimum number of swaps required to make a number divisible by 60
- Find the minimum number to be added to N to make it a prime number
- Minimum number of distinct powers of 2 required to express a given binary number
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number n
- Minimum and maximum number of digits required to be removed to make a given number divisible by 3
- Remove minimum numbers from the array to get minimum OR value
- Minimum distance between the maximum and minimum element of a given Array
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Number of factors of very large number N modulo M where M is any prime number
- Find minimum number of coins that make a given value
- Find minimum number of merge operations to make an array palindrome
- Minimum number of increment/decrement operations such that array contains all elements from 1 to N