Maximize count of planes that can be stopped per second with help of given initial position and speed
Given two arrays A[] and B[] consisting of N integers where A[i] represents the initial position of the ith plane and B[i] is the speed at which the plane is landing, the task is to print the number of the plane that can be stopped from landing by shooting an aircraft at every second.
Examples:
Input: A[] = {1, 3, 5, 4, 8}, B[] = {1, 2, 2, 1, 2}
Output: 4
Explanation:
- At second 1, shoot the plane at index 0, the positions of the planes are now A[] = {_, 1, 3, 3, 6}.
- At second 2, shoot the plane at index 1, the positions of the planes are now A[] = {_, _, 1, 2, 4}.
- At second 3, shoot the plane at index 2, the positions of the planes are now A[] = {_, _, _, 1, 2}.
- At second 4, shoot the plane at index 4 or 5, the positions of the planes are now A[] = {_, _, _, _, _}.
Therefore, a total of 4 planes can be stopped from landing.
Input: A[] = {2, 8, 2, 3, 1}, B[] = {1, 4, 2, 2, 2}
Output: 2
Approach: The given problem can be solved based on the following observations:
- It can be observed that the count of planes that can be stopped is the set of distinct times needed to land for each plane, as at the same time, only one plane can be destroyed.
- The time needed for a plane to land is A[i] / B[i] can be calculated using the formula: Time = Distance / Velocity
Follow the steps below to solve the problem:
- Initialize a Hash-Set, say S that stores the times needed for plane landing.
- Iterate over a range [0, N] using the variable i and perform the following tasks:
- Initialize a variable, say t as 1 if the value of A[i]%B[i] is greater than 0. Otherwise, update the variable t as 0.
- Add the value of A[i]/B[i] into the variable, say t.
- Add the value of t into the HashSet S.
- After performing the above steps, return the size of the HashSet as the resultant answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPlanes( int A[], int B[], int N)
{
set< int > St;
for ( int i = 0; i < N; i++) {
int t = (A[i] % B[i] > 0) ? 1 : 0;
t += (A[i] / B[i]) + t;
St.insert(t);
}
return St.size();
}
int main() {
int A[] = { 1, 3, 5, 4, 8 };
int B[] = { 1, 2, 2, 1, 2 };
int N = sizeof (A)/ sizeof (A[0]);
cout << maxPlanes(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxPlanes( int [] A, int [] B)
{
Set<Integer> St = new HashSet<>();
for ( int i = 0 ; i < A.length; i++) {
int t = (A[i] % B[i] > 0 ) ? 1 : 0 ;
t += (A[i] / B[i]) + t;
St.add(t);
}
return St.size();
}
public static void main(String[] args)
{
int [] A = { 1 , 3 , 5 , 4 , 8 };
int [] B = { 1 , 2 , 2 , 1 , 2 };
System.out.println(
maxPlanes(A, B));
}
}
|
Python3
def maxPlanes(A, B, N):
St = set ()
for i in range (N):
t = 1 if (A[i] % B[i] > 0 ) else 0
t + = (A[i] / / B[i]) + t
St.add(t)
return len (St)
A = [ 1 , 3 , 5 , 4 , 8 ]
B = [ 1 , 2 , 2 , 1 , 2 ]
N = len (A)
print (maxPlanes(A, B, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int maxPlanes( int [] A, int [] B)
{
HashSet< int > St = new HashSet< int >();
for ( int i = 0; i < A.Length; i++) {
int t = (A[i] % B[i] > 0) ? 1 : 0;
t += (A[i] / B[i]) + t;
St.Add(t);
}
return St.Count;
}
static public void Main (){
int [] A = { 1, 3, 5, 4, 8 };
int [] B = { 1, 2, 2, 1, 2 };
Console.WriteLine(
maxPlanes(A, B));
}
}
|
Javascript
<script>
function maxPlanes(A, B)
{
let St = new Set();
for (let i = 0; i < A.length; i++) {
let t = (A[i] % B[i] > 0) ? 1 : 0;
t += Math.floor(A[i] / B[i]) + t;
St.add(t);
}
return St.size;
}
let A = [ 1, 3, 5, 4, 8 ];
let B = [ 1, 2, 2, 1, 2 ];
document.write(
maxPlanes(A, B));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
28 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...