You are given a 0-indexed array of distinct integers nums.
There is an element in nums that has the lowest value and an element that has the highest value. We call them the minimum and maximum respectively. Your goal is to remove both these elements from the array.
A deletion is defined as either removing an element from the front of the array or removing an element from the back of the array.
Return the minimum number of deletions it would take to remove both the minimum and maximum element from the array.
Example 1:
Input: nums = [2,10,7,5,4,1,8,6] Output: 5 Explanation: The minimum element in the array is nums[5], which is 1. The maximum element in the array is nums[1], which is 10. We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back. This results in 2 + 3 = 5 deletions, which is the minimum number possible.
Example 2:
Input: nums = [0,-4,19,1,8,-2,-3,5] Output: 3 Explanation: The minimum element in the array is nums[1], which is -4. The maximum element in the array is nums[2], which is 19. We can remove both the minimum and maximum by removing 3 elements from the front. This results in only 3 deletions, which is the minimum number possible.
Example 3:
Input: nums = [101] Output: 1 Explanation: There is only one element in the array, which makes it both the minimum and maximum element. We can remove it with 1 deletion.
Constraints:
1 <= nums.length <= 105-105 <= nums[i] <= 105nums are distinct.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The most straightforward way to solve this is to look at every possible way to remove one smallest and one largest number from the set. We'll then pick the best removal strategy. It's like trying every single combination until you find the winning one.
Here's how the algorithm would work step-by-step:
def removing_minimum_and_maximum_from_array(numbers):
array_length = len(numbers)
minimum_skips = array_length # Initialize with worst case
for minimum_index in range(array_length):
for maximum_index in range(array_length):
# Ensure we're considering distinct elements
if minimum_index == maximum_index:
continue
# Determine indices of min and max values.
minimum_value = numbers[minimum_index]
maximum_value = numbers[maximum_index]
is_valid_pair = True
for index in range(array_length):
if index != minimum_index and numbers[index] < minimum_value:
is_valid_pair = False
break
if index != maximum_index and numbers[index] > maximum_value:
is_valid_pair = False
break
if not is_valid_pair:
continue
# Calculate skips from left and right
left_skips = max(minimum_index, maximum_index) + 1
# To the right, the elements we remove are also skipped
right_skips = array_length - min(minimum_index, maximum_index)
current_skips = min(left_skips, right_skips)
# Keep track of the best case skips
if current_skips < minimum_skips:
minimum_skips = current_skips
return minimum_skipsThe trick is to realize you only need to check removing elements from the start, from the end, or from both. We efficiently figure out the costs of these scenarios and pick the cheapest one.
Here's how the algorithm would work step-by-step:
def removing_minimum_and_maximum(numbers):
array_length = len(numbers)
minimum_value_index = numbers.index(min(numbers))
maximum_value_index = numbers.index(max(numbers))
# Find the furthest index to remove from the start.
farthest_index = max(minimum_value_index, maximum_value_index)
# Find the closest index to remove from the end.
closest_index = min(minimum_value_index, maximum_value_index)
removal_from_start_cost = farthest_index + 1
removal_from_end_cost = array_length - closest_index
# Calculate the cost of removing from both ends.
removal_from_both_ends_cost = (minimum_value_index + 1) + (array_length - maximum_value_index)
# Calculate cost if max index is smaller than min index
if maximum_value_index < minimum_value_index:
removal_from_both_ends_cost = (maximum_value_index + 1) + (array_length - minimum_value_index)
# Determine the minimum number of removals required
minimum_removals = min(removal_from_start_cost, removal_from_end_cost, removal_from_both_ends_cost)
return minimum_removals| Case | How to Handle |
|---|---|
| Null or undefined input array | Return an empty list or throw an IllegalArgumentException to prevent NullPointerException. |
| Empty array | Return 0 since no removal is needed. |
| Array with one element | Return 0 since the single element is both min and max and needs to be removed, thus zero operations. |
| Array with two elements | Return 1 since removing both min and max is optimal which requires one operation. |
| Array with all elements being the same | Return Math.min(first element index + 1, array length - first element index) as either from the beginning or the end will be the same. |
| Array with a large number of elements, potentially causing integer overflow when calculating indices or distances. | Use long data type for index calculations if the array size is large to avoid integer overflow. |
| Array with negative numbers, zeros, and positive numbers mixed | The standard min/max finding algorithms work correctly regardless of the number signs. |
| The minimum and maximum values are at the beginning and the end of the array respectively (or vice-versa). | Return 1, as we only need to remove elements from one end. |