What is the approach to solve the given problem Giant Army?

Modified on Tue, 28 Mar, 2023 at 4:40 PM

What is the issue? 

I don’t know how to solve this problem.

Do you understand the problem?

No?

Let’s discuss the problem first:

Problem describes a scenario where a planet named GiantX is preparing for a war with another planet, and different army battalions are recruiting warriors to save the planet. Each battalion has a different minimum height requirement for selecting warriors. There are N people who want to join the army and their heights are given. The task is to determine, for each battalion, how many people are eligible to join the army based on the given height requirements. 


Check: Do you have any approach to solve it now?

Yes, Let’s try it first then.


No?

Let’s discuss the approach.


To solve this problem we can use binary search. Binary search is a search algorithm that is used to find the position of a target value within a sorted array. It works by repeatedly dividing the search interval in half until the target value is found. 


In this particular problem, we can use lower bound binary search.It is a variant of binary search that returns the index of the first element in the array that is greater than or equal to the target value. If the target value is greater than all the elements in the array, the function returns the size of the array. This can be useful in the given problem as we need to find the number of people who are eligible to join the army based on their height. If we sort the array of heights, we can use lower bound binary search to find the number of people whose height is greater than or equal to the minimum height requirement of a battalion.



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article