
Removing Minimum and Maximum From Array (medium)
Problem Statement
Determine the minimum number of deletions required to remove the smallest and the largest elements from an array of integers.
In each deletion, you are allowed to remove either the first (leftmost) or the last (rightmost) element of the array.
Examples
-
Example 1:
- Input:
[3, 2, 5, 1, 4] - Expected Output:
3 - Justification: The smallest element is
1and the largest is5. Removing4,1, and then5(or5,4, and then1) in three moves is the most efficient strategy.
- Input:
-
Example 2:
- Input:
[7, 5, 6, 8, 1] - Expected Output:
2 - Justification: Here,
1is the smallest, and8is the largest. Removing1and then8in two moves is the optimal strategy.
- Input:
-
Example 3:
- Input:
[2, 4, 10, 1, 3, 5] - Expected Output:
4 - Justification: The smallest is
1and the largest is10. One strategy is to remove2,4,10, and then1in four moves.
- Input:
Constraints:
- 1 <= nums.length <= 10<sup>5</sup>
- -10<sup>5</sup> <= nums[i] <= 10<sup>5</sup>
- The integers in nums are distinct.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory