# Maximum number of bomb blasts that may occur before the thief gets caught

• Difficulty Level : Expert
• Last Updated : 13 Jul, 2021

Given an array, arr[] of M integers, where the ith element represents the time after which the ith bomb will blast after dropping it, and three integers N, X, and Y representing the number of adjacent continuous cells on the X-coordinate, and the initial cell positions of a thief and police. The task is to find the maximum number of bomb blasts that may occur before the thief gets caught if, at every second, the thief can either drop a bomb or move to the left or right of an existing cell not visited by the police.

Examples:

Input: arr[] = {1, 4}, N = 7, X = 3, Y = 6
Output: 2
Explanation:
One possible way is:

1. At t = 0: Thief drops the bomb of activating time equal to 4. Meanwhile, the police move one cell towards the thief. Thereafter, the positions of the thief and police are 3 and 5 respectively.
2. At t = 1: The police move one cell towards the thief and the thief moves one cell to its left. Thereafter, the positions of the thief and police are 2 and 4 respectively.
3. At t = 2: The police move one cell towards the thief and the thief moves one cell to its left. Thereafter, the positions of the thief and police are 1 and 3 respectively.
4. At t = 3: The police move one cell towards the thief and the thief drops the bomb of activating time equal to 1. Thereafter, the positions of the thief and police are 1 and 2 respectively.
5. At t = 4: The bombs dropped at time (t= 3, and t = 0) blasts. Now the thief cannot move to any cell, and it does not have any bombs left. The police move one cell towards the thief, finally catching it at cell 1.

Therefore, the maximum bomb blasts that occurred before the thief got caught is 2.

Input: arr[] = {5, 1}, N = 7, X = 3, Y = 6
Output: 1

Approach: The given problem can be solved based on the following observations:

1. If both police and thief move optimally, then at every second the police will move towards the thief. Therefore, the maximum time the thief has before getting caught is the distance between their positions.
2. It can be observed that the best choice is to drop the bomb with more activating time first than the less activating time. If a bomb with less time is dropped first and then dropping the bomb with more activating time may exceed the time that the thief has before getting caught.

Follow the steps below to solve the problem:

• Sort the array arr[] in descending order.
• Initialize two variables, say count and time with value 0 to store the maximum count of the bomb blast that may occur and the time passed.
• Find the absolute difference between X and Y and store it in a variable, say maxSec.
• Iterate in the range[0, M-1], using the variable i, and do the following steps:
• If the sum of the current element and the time is less than or equal to the maxSec then increment count and time by 1.
• After the above step, update the count as count = min(count, abs(X-Y)-1).
• Finally, after completing the above steps, print the value of count as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum number``// of bomb that can be blasted before``// the thief gets caught``int` `findMaxBomb(``int` `N, ``int` `M, ``int` `X, ``int` `Y, ``int` `arr[])``{``    ``// Sort the array arr[] in``    ``// descending order``    ``sort(arr, arr + M, greater<``int``>());` `    ``// Stores the maxtime the thief``    ``// has before getting caught``    ``int` `maxSec;` `    ``// If Y is less than X``    ``if` `(Y < X) {``        ``maxSec = N - Y;``    ``}``    ``// Otherwise``    ``else` `{``        ``maxSec = Y - 1;``    ``}` `    ``// Stores the current``    ``// second``    ``int` `time` `= 1;` `    ``// Stores the count of``    ``// bomb blasts``    ``int` `count = 0;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < M; i++) {` `        ``// If arr[i]+time is less``        ``// than or equal to the``        ``// maxSec``        ``if` `(arr[i] + ``time` `<= maxSec) {``            ``// Increment time and``            ``// count by 1``            ``time``++;``            ``count++;``        ``}``    ``}` `    ``// Update count``    ``count = min(count, ``abs``(X - Y) - 1);` `    ``// Return the value of count``    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `N = 7, X = 3, Y = 6;``    ``int` `arr[] = { 1, 4 };``    ``int` `M = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function Call``    ``cout << findMaxBomb(N, M, X, Y, arr);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.Arrays;``import` `java.util.Collections;` `public` `class` `GFG {` `    ``// Function to find the maximum number``    ``// of bomb that can be blasted before``    ``// the thief gets caught``    ``static` `int` `findMaxBomb(``int` `N, ``int` `M, ``int` `X, ``int` `Y,``                           ``Integer arr[])``    ``{``        ``// Sort the array arr[] in``        ``// descending order``        ``Arrays.sort(arr, Collections.reverseOrder());` `        ``// Stores the maxtime the thief``        ``// has before getting caught``        ``int` `maxSec;` `        ``// If Y is less than X``        ``if` `(Y < X) {``            ``maxSec = N - Y;``        ``}``        ``// Otherwise``        ``else` `{``            ``maxSec = Y - ``1``;``        ``}` `        ``// Stores the current``        ``// second``        ``int` `time = ``1``;` `        ``// Stores the count of``        ``// bomb blasts``        ``int` `count = ``0``;` `        ``// Traverse the array arr[]``        ``for` `(``int` `i = ``0``; i < M; i++) {` `            ``// If arr[i]+time is less``            ``// than or equal to the``            ``// maxSec``            ``if` `(arr[i] + time <= maxSec) {``                ``// Increment time and``                ``// count by 1``                ``time++;``                ``count++;``            ``}``        ``}` `        ``// Update count``        ``count = Math.min(count, Math.abs(X - Y) - ``1``);` `        ``// Return the value of count``        ``return` `count;``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Given Input``        ``int` `N = ``7``, X = ``3``, Y = ``6``;``        ``Integer arr[] = { ``1``, ``4` `};``        ``int` `M = arr.length;` `        ``// Function Call``        ``System.out.println(findMaxBomb(N, M, X, Y, arr));``    ``}``}` `// This code is contributed by abhinavjain194`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum number``# of bomb that can be blasted before``# the thief gets caught``def` `findMaxBomb(N, M, X, Y, arr):``    ` `    ``# Sort the array arr[] in``    ``# descending order``    ``arr.sort(reverse ``=` `True``)` `    ``# Stores the maxtime the thief``    ``# has before getting caught``    ``maxSec ``=` `0` `    ``# If Y is less than X``    ``if` `(Y < X):``        ``maxSec ``=` `N ``-` `Y` `    ``# Otherwise``    ``else``:``        ``maxSec ``=` `Y ``-` `1` `    ``# Stores the current``    ``# second``    ``time ``=` `1` `    ``# Stores the count of``    ``# bomb blasts``    ``count ``=` `0` `    ``# Traverse the array arr[]``    ``for` `i ``in` `range``(M):``        ` `        ``# If arr[i]+time is less``        ``# than or equal to the``        ``# maxSec``        ``if` `(arr[i] ``+` `time <``=` `maxSec):``            ` `            ``# Increment time and``            ``# count by 1``            ``time ``+``=` `1``            ``count ``+``=` `1` `    ``# Update count``    ``count ``=` `min``(count, ``abs``(X ``-` `Y) ``-` `1``)` `    ``# Return the value of count``    ``return` `count` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given Input``    ``N ``=` `7``    ``X ``=` `3``    ``Y ``=` `6``    ``arr ``=` `[ ``1``, ``4` `]``    ``M ``=` `len``(arr)` `    ``# Function Call``    ``print``(findMaxBomb(N, M, X, Y, arr))` `# This code is contributed by ipg2016107`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find the maximum number``// of bomb that can be blasted before``// the thief gets caught``static` `int` `findMaxBomb(``int` `N, ``int` `M, ``int` `X,``                       ``int` `Y, ``int``[] arr)``{``    ` `    ``// Sort the array arr[] in``    ``// descending order``    ``Array.Sort(arr);` `    ``// Reverse array``    ``Array.Reverse(arr);` `    ``// Stores the maxtime the thief``    ``// has before getting caught``    ``int` `maxSec;` `    ``// If Y is less than X``    ``if` `(Y < X)``    ``{``        ``maxSec = N - Y;``    ``}``    ` `    ``// Otherwise``    ``else``    ``{``        ``maxSec = Y - 1;``    ``}` `    ``// Stores the current``    ``// second``    ``int` `time = 1;` `    ``// Stores the count of``    ``// bomb blasts``    ``int` `count = 0;` `    ``// Traverse the array arr[]``    ``for``(``int` `i = 0; i < M; i++)``    ``{``        ` `        ``// If arr[i]+time is less``        ``// than or equal to the``        ``// maxSec``        ``if` `(arr[i] + time <= maxSec)``        ``{``            ` `            ``// Increment time and``            ``// count by 1``            ``time++;``            ``count++;``        ``}``    ``}` `    ``// Update count``    ``count = Math.Min(count, Math.Abs(X - Y) - 1);` `    ``// Return the value of count``    ``return` `count;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ` `    ``// Given Input``    ``int` `N = 7, X = 3, Y = 6;``    ``int``[] arr = { 1, 4 };``    ``int` `M = arr.Length;` `    ``// Function Call``    ``Console.WriteLine(findMaxBomb(N, M, X, Y, arr));``}``}` `// This code is contributed by target_2`

## Javascript

 ``

Output

`2`

Time Complexity: O(M*log(M)), where M is the size of the array arr[].
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up