# Maximize count of planes that can be stopped per second with help of given initial position and speed

• Difficulty Level : Easy
• Last Updated : 28 Dec, 2022

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:

1. At second 1, shoot the plane at index 0, the positions of the planes are now A[] = {_, 1, 3, 3, 6}.
2. At second 2, shoot the plane at index 1, the positions of the planes are now A[] = {_, _, 1, 2, 4}.
3. At second 3, shoot the plane at index 2, the positions of the planes are now A[] = {_, _, _, 1, 2}.
4. 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++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find maximum number` `// of planes that can be stopped` `// from landing` `int` `maxPlanes(``int` `A[], ``int` `B[], ``int` `N)` `{` `  `  `    ``// Stores the times needed for` `    ``// landing for each plane` `    ``set<``int``> St;`   `    ``// Iterate over the arrays` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Stores the time needed for` `        ``// landing of current plane` `        ``int` `t = (A[i] % B[i] > 0) ? 1 : 0;`   `        ``// Update the value of t` `        ``t += (A[i] / B[i]) + t;`   `        ``// Append the t in set St` `        ``St.insert(t);` `    ``}`   `    ``// Return the answer` `    ``return` `St.size();` `}`   ` ``// Driver Code` `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;` `}`   `// This code is contributed by Dharanendra L V.`

## Java

 `// Java program for the above approach`   `import` `java.util.*;`   `class` `GFG {`   `    ``// Function to find maximum number` `    ``// of planes that can be stopped` `    ``// from landing` `    ``static` `int` `maxPlanes(``int``[] A, ``int``[] B)` `    ``{` `        ``// Stores the times needed for` `        ``// landing for each plane` `        ``Set St = ``new` `HashSet<>();`   `        ``// Iterate over the arrays` `        ``for` `(``int` `i = ``0``; i < A.length; i++) {`   `            ``// Stores the time needed for` `            ``// landing of current plane` `            ``int` `t = (A[i] % B[i] > ``0``) ? ``1` `: ``0``;`   `            ``// Update the value of t` `            ``t += (A[i] / B[i]) + t;`   `            ``// Append the t in set St` `            ``St.add(t);` `        ``}`   `        ``// Return the answer` `        ``return` `St.size();` `    ``}`   `    ``// Driver Code` `    ``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

 `# Python program for the above approach`   `# Function to find maximum number` `# of planes that can be stopped` `# from landing` `def` `maxPlanes(A, B, N):` `    `  `    ``# Stores the times needed for` `    ``# landing for each plane` `    ``St ``=` `set``()` `    `  `    ``# Iterate over the arrays` `    ``for` `i ``in` `range``(N):` `        `  `        ``# Stores the time needed for` `        ``# landing of current plane` `        ``t ``=` `1` `if` `(A[i] ``%` `B[i] > ``0``) ``else` `0` `        `  `        ``# Update the value of t` `        ``t ``+``=` `(A[i] ``/``/` `B[i]) ``+` `t` `        `  `        ``# Append the t in set St` `        ``St.add(t)` `        `  `    ``# Return the answer` `    ``return` `len``(St)`   `# Driver Code` `A ``=` `[ ``1``, ``3``, ``5``, ``4``, ``8` `]` `B ``=` `[ ``1``, ``2``, ``2``, ``1``, ``2` `]` `N ``=` `len``(A)` `print``(maxPlanes(A, B, N))`   `# This code is contributed by shivanisinghss2110`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG` `{` `  `  `    ``// Function to find maximum number` `    ``// of planes that can be stopped` `    ``// from landing` `    ``static` `int` `maxPlanes(``int``[] A, ``int``[] B)` `    ``{` `      `  `        ``// Stores the times needed for` `        ``// landing for each plane` `         ``HashSet<``int``> St = ``new` `HashSet<``int``>();`   `        ``// Iterate over the arrays` `        ``for` `(``int` `i = 0; i < A.Length; i++) {`   `            ``// Stores the time needed for` `            ``// landing of current plane` `            ``int` `t = (A[i] % B[i] > 0) ? 1 : 0;`   `            ``// Update the value of t` `            ``t += (A[i] / B[i]) + t;`   `            ``// Append the t in set St` `            ``St.Add(t);` `        ``}`   `        ``// Return the answer` `        ``return` `St.Count;` `    ``}` `  `  `  ``// Driver code` `    ``static` `public` `void` `Main (){`   `       ``int``[] A = { 1, 3, 5, 4, 8 };` `        ``int``[] B = { 1, 2, 2, 1, 2 };` `        ``Console.WriteLine(` `            ``maxPlanes(A, B));` `    ``}` `}`   `// This code is contributed by Potta Lokesh`

## Javascript

 ``

Output:

`3`

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles