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: Min 1 is at index 5, max 10 at index 1. Cheapest is 2 deletions from the front and 3 from the back = 5.
Example 2
Input: nums = [0,-4,19,1,8,-2,-3,5]
Output: 3
Explanation: Min -4 at index 1, max 19 at index 2. Both fall within the first 3 elements, so 3 front deletions suffice.
Example 3
Input: nums = [101]
Output: 1
Explanation: A single element is both min and max; one deletion removes it.
Only the positions of the min and max matter. Deletions always peel from the two ends, so once we know how far each extreme sits from the front and the back, there are just three ways to remove both — take the cheapest.
Run checks all cases above. Submit evaluates all test cases.