2091. Removing Minimum and Maximum From Array LeetCode Solution
In this guide, you will get 2091. Removing Minimum and Maximum From Array LeetCode Solution with the best time and space complexity. The solution to Removing Minimum and Maximum From Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Removing Minimum and Maximum From Array solution in C++
- Removing Minimum and Maximum From Array solution in Java
- Removing Minimum and Maximum From Array solution in Python
- Additional Resources

Problem Statement of Removing Minimum and Maximum From Array
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] <= 105
The integers in nums are distinct.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2091. Removing Minimum and Maximum From Array LeetCode Solution in C++
class Solution {
public:
int minimumDeletions(vector<int>& nums) {
const int n = nums.size();
int a = ranges::min_element(nums) - nums.begin();
int b = ranges::max_element(nums) - nums.begin();
if (a > b)
swap(a, b);
return min({a + 1 + n - b, b + 1, n - a});
}
};
/* code provided by PROGIEZ */
2091. Removing Minimum and Maximum From Array LeetCode Solution in Java
class Solution {
public int minimumDeletions(int[] nums) {
final int n = nums.length;
int mn = Integer.MAX_VALUE;
int mx = Integer.MIN_VALUE;
int minIndex = -1;
int maxIndex = -1;
for (int i = 0; i < n; ++i) {
if (nums[i] < mn) {
mn = nums[i];
minIndex = i;
}
if (nums[i] > mx) {
mx = nums[i];
maxIndex = i;
}
}
final int a = Math.min(minIndex, maxIndex);
final int b = Math.max(minIndex, maxIndex);
// min(delete from front and back,
// delete from front,
// delete from back)
return Math.min(a + 1 + n - b, Math.min(b + 1, n - a));
}
}
// code provided by PROGIEZ
2091. Removing Minimum and Maximum From Array LeetCode Solution in Python
class Solution:
def minimumDeletions(self, nums: list[int]) -> int:
n = len(nums)
a = nums.index(min(nums))
b = nums.index(max(nums))
if a > b:
a, b = b, a
return min(a + 1 + n - b, b + 1, n - a)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.