Festival Drone Altitude Limit
You are given two matching arrays: altitudes and stable. Each altitude has already been tested, and stable[i] tells you whether flying at altitudes[i] is still safe.
Your job is to return the highest altitude that is still stable. If every test fails, or if there are no test results at all, the answer should be -1.
For example, if altitudes = [50,80,120] and stable = [true,true,false], the answer is 80 because 80 is the last altitude that still works before the failure at 120. If altitudes = [30,60] and stable = [false,false], the answer is -1 because none of the tested heights are safe.
So the task is to return the last altitude whose matching stability value is true.
Example Input & Output
Without test data, no safe altitude can be confirmed.
All tested altitudes fail, so no safe flight ceiling exists.
Altitude 80 is the last stable value before failure at 120.
Algorithm Flow

Solution Approach
This is a classic last true binary search. The test results are ordered by altitude, so once higher tests start failing, the last safe altitude must be somewhere to the left of that failure point.
We keep two pointers, left and right, and also remember the best safe altitude found so far:
At each step, check the middle result. If stable[mid] is true, that altitude is valid, but there may still be a higher safe altitude later, so we save it and move right:
When stable[mid] is false, we know the last safe altitude must be earlier, so we search the left side.
When the loop ends, answer is either the highest stable altitude or -1 if no safe test was found. This runs in O(log n) time and uses O(1) extra space.
Best Answers
class Solution {
public int festival_drone_altitude_limit(int[] nums, int limit) {
int count = 0;
for (int x : nums) if (x < limit) count++;
return count;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
