#2091
Removing Minimum and Maximum From Array
specialist · 775 · lc medium +31 · verified · 56.3% accepted · 1,040 likes · top 50%
Description
You are given a 0-indexed array of distinct integers nums.
The array contains one element with the smallest value and one with the largest value — these are the minimum and maximum. Your task is to remove both of them from the array.
Each removal operation consists of deleting either the first or the last element of the array.
Return the minimum total number of removal operations needed to eliminate both the minimum and maximum elements 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.
Code
1
2
3