Related Articles

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

• Last Updated : 03 Aug, 2021

Given two arrays dist[] and speed[] consisting of N integers, where dist[i] is the initial distance of the ith giant from the city and speed[i] is the speed of the ith giant. One giant can be eliminated every minute. Therefore, at any time 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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: dist[] = [1, 3, 4], speed[] = [1, 1, 1]
Output: 3
Explanation: 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:
• Push the value of dist[i]/speed[i] into the vector timezone[].
• 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.
• 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 ``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);` `    ``int` `speed[] = { 1, 1, 1 };``    ``int` `M = ``sizeof``(speed) / ``sizeof``(speed);` `    ``// 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 timezone = ``new` `Vector();` `    ``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

 ``

Output:
`3`

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

My Personal Notes arrow_drop_up