# Count maximum number of giants that can be destroyed before they reach the city

Given two arrays **dist[]** and **speed[]** consisting of **N** integers, where **dist[i]** is the initial distance of the **i ^{th}** giant from the city and

**speed[i]**is the speed of the

**i**giant. One giant can be eliminated every minute. Therefore, at any time

^{th}**t**, at most

**t**giants can be killed. If more giants are able to reach the city in time

**t**, then the game is over. The task is to find the maximum number of giants that can be eliminated before losing, or

**N**if all of the giants can be eliminated before they reach the city.

**Examples:**

Input:dist[] = [1, 3, 4], speed[] = [1, 1, 1]Output:3Explanation:At the start of minute 0, the distances of the giants are [1, 3, 4]. The first giant is eliminated.

At the start of 1st minute, the distances of the giants are [X, 2, 3]. No giant is eliminated.

At the start of 2nd minute, the distances of the giants are [X, 1, 2]. The second giant is eliminated.

At the start of 3rd minute, the distances of the giants are [X, X, 1]. The third giant is eliminated.

All 3 giants can be eliminated.

Input:dist[] = [1, 1, 2, 3], speed[] = [1, 1, 1, 1]Output:1

**Approach:** The idea is to use the greedy approach to solve the problem. Find the time for each giant to come into the city and try to destroy the giant with the least possible time of approaching. Follow the steps below to solve the problem:

- Initialize a vector
**timezone[]**to store the times. - Iterate over the range
**[0, N]**using the variable**i**and perform the following steps: - Sort the vector
**timezone[]**in ascending order. - Initialize the variables
**curr_time**as**0**to store the current time and**killcount**as**0**to store the number of giants killed. - Iterate over the range
**[0, N]**using the variable**i**and perform the following steps:- If
**timezone[i]**is less than**curr_time**, then break the loop. - Else, increase the count of
**curr_time**and**killcount**by**1.**

- If
- After performing the above steps, return the value of
**killcount**as the answer.

Below is the implementation of the above approach.

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the maximum number` `// of giants that can be destroyed` `int` `eliminateMaximum(` `int` `dist[], ` `int` `N,` ` ` `int` `speed[])` `{` ` ` `// Make a vector of time and` ` ` `// store the time corresponding` ` ` `// to its distance and speed` ` ` `vector<` `double` `> timezone;` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `timezone.push_back((` `double` `)dist[i]` ` ` `/ (` `double` `)speed[i]);` ` ` `}` ` ` `// Sort time in ascending order` ` ` `sort(timezone.begin(), timezone.end());` ` ` `// Stores the time at each instant` ` ` `int` `Curr_time = 0;` ` ` `// Stores count of giants killed` ` ` `int` `killcount = 0;` ` ` `for` `(` `auto` `i : timezone) {` ` ` `if` `(i <= Curr_time) {` ` ` `// Game is lost` ` ` `break` `;` ` ` `}` ` ` `else` `{` ` ` `Curr_time++;` ` ` `killcount++;` ` ` `}` ` ` `}` ` ` `return` `killcount;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given input` ` ` `int` `dist[] = { 1, 3, 4 };` ` ` `int` `N = ` `sizeof` `(dist) / ` `sizeof` `(dist[0]);` ` ` `int` `speed[] = { 1, 1, 1 };` ` ` `int` `M = ` `sizeof` `(speed) / ` `sizeof` `(speed[0]);` ` ` `// function call` ` ` `cout << eliminateMaximum(dist, N, speed);` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to count the maximum number` `// of giants that can be destroyed` `static` `int` `eliminateMaximum(` `int` `dist[], ` `int` `N,` ` ` `int` `speed[])` `{` ` ` ` ` `// Make a vector of time and` ` ` `// store the time corresponding` ` ` `// to its distance and speed` ` ` `Vector<Double> timezone = ` `new` `Vector<Double>();` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) {` ` ` `timezone.add((` `double` `)dist[i]` ` ` `/ (` `double` `)speed[i]);` ` ` `}` ` ` `// Sort time in ascending order` ` ` `Collections.sort(timezone);` ` ` `// Stores the time at each instant` ` ` `int` `Curr_time = ` `0` `;` ` ` `// Stores count of giants killed` ` ` `int` `killcount = ` `0` `;` ` ` `for` `(Double i : timezone) {` ` ` `if` `(i <= Curr_time) {` ` ` `// Game is lost` ` ` `break` `;` ` ` `}` ` ` `else` `{` ` ` `Curr_time++;` ` ` `killcount++;` ` ` `}` ` ` `}` ` ` `return` `killcount;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `// Given input` ` ` `int` `dist[] = { ` `1` `, ` `3` `, ` `4` `};` ` ` `int` `N = dist.length;` ` ` `int` `speed[] = { ` `1` `, ` `1` `, ` `1` `};` ` ` `int` `M = speed.length;` ` ` `// function call` ` ` `System.out.print(eliminateMaximum(dist, N, speed));` `}` `}` `// This code is contributed by shikhasingrajput` |

## Python3

`# Python program for the above approach` `# Function to count the maximum number` `# of giants that can be destroyed` `def` `eliminateMaximum(dist, N, speed):` ` ` `# Make a vector of time and` ` ` `# store the time corresponding` ` ` `# to its distance and speed` ` ` `timezone ` `=` `[]` ` ` `for` `i ` `in` `range` `(N):` ` ` `timezone.append(dist[i] ` `/` `speed[i])` ` ` `# Sort time in ascending order` ` ` `timezone.sort()` ` ` `# Stores the time at each instant` ` ` `Curr_time ` `=` `0` ` ` `# Stores count of giants killed` ` ` `killcount ` `=` `0` ` ` `for` `i ` `in` `timezone:` ` ` `if` `(i <` `=` `Curr_time) :` ` ` `# Game is lost` ` ` `break` ` ` `else` `:` ` ` `Curr_time ` `+` `=` `1` ` ` `killcount ` `+` `=` `1` ` ` ` ` `return` `killcount` `# Driver Code` `# Given input` `dist ` `=` `[` `1` `, ` `3` `, ` `4` `]` `N ` `=` `len` `(dist)` `speed ` `=` `[` `1` `, ` `1` `, ` `1` `]` `M ` `=` `len` `(speed)` `# function call` `print` `(eliminateMaximum(dist, N, speed))` `# This code is contributed by gfgking` |

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` `// Function to count the maximum number` `// of giants that can be destroyed` `static` `int` `eliminateMaximum(` `int` `[]dist, ` `int` `N,` ` ` `int` `[]speed)` `{` ` ` `// Make a vector of time and` ` ` `// store the time corresponding` ` ` `// to its distance and speed` ` ` `List<` `double` `> timezone = ` `new` `List<` `double` `>();` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `timezone.Add((` `double` `)dist[i]/speed[i]);` ` ` `}` ` ` `// Sort time in ascending order` ` ` `timezone.Sort();` ` ` `// Stores the time at each instant` ` ` `int` `Curr_time = 0;` ` ` `// Stores count of giants killed` ` ` `int` `killcount = 0;` ` ` `foreach` `(` `double` `i ` `in` `timezone) {` ` ` `if` `(i <= Curr_time) {` ` ` `// Game is lost` ` ` `break` `;` ` ` `}` ` ` `else` `{` ` ` `Curr_time++;` ` ` `killcount++;` ` ` `}` ` ` `}` ` ` `return` `killcount;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` `// Given input` ` ` `int` `[]dist = { 1, 3, 4 };` ` ` `int` `N = dist.Length;` ` ` `int` `[]speed = { 1, 1, 1 };` ` ` `int` `M = speed.Length;` ` ` `// function call` ` ` `Console.Write(eliminateMaximum(dist, N, speed));` `}` `}` `// This code is contributed by ipg2016107.` |

## Javascript

`<script>` ` ` `// JavaScript program for the above approach` ` ` `// Function to count the maximum number` ` ` `// of giants that can be destroyed` ` ` `function` `eliminateMaximum(dist, N, speed)` ` ` `{` ` ` ` ` `// Make a vector of time and` ` ` `// store the time corresponding` ` ` `// to its distance and speed` ` ` `let timezone = [];` ` ` `for` `(let i = 0; i < N; i++) {` ` ` `timezone.push(dist[i] / speed[i]);` ` ` `}` ` ` `// Sort time in ascending order` ` ` `timezone.sort(` `function` `(a, b) { ` `return` `a - b; });` ` ` `// Stores the time at each instant` ` ` `let Curr_time = 0;` ` ` `// Stores count of giants killed` ` ` `let killcount = 0;` ` ` `for` `(let i of timezone) {` ` ` `if` `(i <= Curr_time) {` ` ` `// Game is lost` ` ` `break` `;` ` ` `}` ` ` `else` `{` ` ` `Curr_time++;` ` ` `killcount++;` ` ` `}` ` ` `}` ` ` `return` `killcount;` ` ` `}` ` ` `// Driver Code` ` ` `// Given input` ` ` `let dist = [1, 3, 4];` ` ` `let N = dist.length;` ` ` `let speed = [1, 1, 1];` ` ` `let M = speed.length;` ` ` `// function call` ` ` `document.write(eliminateMaximum(dist, N, speed));` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output:**

3

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