Maximum number of bomb blasts that may occur before the thief gets caught
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:
- 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.
- 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.
- 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.
- 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.
- 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:
- 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int findMaxBomb( int N, int M, int X, int Y, int arr[])
{
sort(arr, arr + M, greater< int >());
int maxSec;
if (Y < X) {
maxSec = N - Y;
}
else {
maxSec = Y - 1;
}
int time = 1;
int count = 0;
for ( int i = 0; i < M; i++) {
if (arr[i] + time <= maxSec) {
time ++;
count++;
}
}
count = min(count, abs (X - Y) - 1);
return count;
}
int main()
{
int N = 7, X = 3, Y = 6;
int arr[] = { 1, 4 };
int M = sizeof (arr) / sizeof (arr[0]);
cout << findMaxBomb(N, M, X, Y, arr);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Collections;
public class GFG {
static int findMaxBomb( int N, int M, int X, int Y,
Integer arr[])
{
Arrays.sort(arr, Collections.reverseOrder());
int maxSec;
if (Y < X) {
maxSec = N - Y;
}
else {
maxSec = Y - 1 ;
}
int time = 1 ;
int count = 0 ;
for ( int i = 0 ; i < M; i++) {
if (arr[i] + time <= maxSec) {
time++;
count++;
}
}
count = Math.min(count, Math.abs(X - Y) - 1 );
return count;
}
public static void main(String[] args)
{
int N = 7 , X = 3 , Y = 6 ;
Integer arr[] = { 1 , 4 };
int M = arr.length;
System.out.println(findMaxBomb(N, M, X, Y, arr));
}
}
|
Python3
def findMaxBomb(N, M, X, Y, arr):
arr.sort(reverse = True )
maxSec = 0
if (Y < X):
maxSec = N - Y
else :
maxSec = Y - 1
time = 1
count = 0
for i in range (M):
if (arr[i] + time < = maxSec):
time + = 1
count + = 1
count = min (count, abs (X - Y) - 1 )
return count
if __name__ = = '__main__' :
N = 7
X = 3
Y = 6
arr = [ 1 , 4 ]
M = len (arr)
print (findMaxBomb(N, M, X, Y, arr))
|
C#
using System;
class GFG{
static int findMaxBomb( int N, int M, int X,
int Y, int [] arr)
{
Array.Sort(arr);
Array.Reverse(arr);
int maxSec;
if (Y < X)
{
maxSec = N - Y;
}
else
{
maxSec = Y - 1;
}
int time = 1;
int count = 0;
for ( int i = 0; i < M; i++)
{
if (arr[i] + time <= maxSec)
{
time++;
count++;
}
}
count = Math.Min(count, Math.Abs(X - Y) - 1);
return count;
}
public static void Main(String[] args)
{
int N = 7, X = 3, Y = 6;
int [] arr = { 1, 4 };
int M = arr.Length;
Console.WriteLine(findMaxBomb(N, M, X, Y, arr));
}
}
|
Javascript
<script>
function findMaxBomb(N, M, X, Y, arr) {
arr.sort( function (a, b) { return b - a })
let maxSec;
if (Y < X) {
maxSec = N - Y;
}
else {
maxSec = Y - 1;
}
let time = 1;
let count = 0;
for (let i = 0; i < M; i++) {
if (arr[i] + time <= maxSec) {
time++;
count++;
}
}
count = Math.min(count, Math.abs(X - Y) - 1);
return count;
}
let N = 7, X = 3, Y = 6;
let arr = [1, 4];
let M = arr.length;
document.write(findMaxBomb(N, M, X, Y, arr));
</script>
|
Time Complexity: O(M*log(M)), where M is the size of the array arr[].
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...