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.

&nbsp;
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.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	-105 &lt;= nums[i] &lt;= 105
	The integers in nums are distinct.


class Solution:
    def minimumDeletions(self, nums: List[int]) -> int:
        minFromFront = nums.index(min(nums))
        maxFromFront = nums.index(max(nums))
        
        minFromBack = len(nums) - minFromFront - 1
        maxFromBack = len(nums) - maxFromFront - 1 
        
        return min(max(minFromFront, maxFromFront) + 1,  # Case 1
                   max(minFromBack, maxFromBack) + 1,    # Case 2
                   minFromBack + maxFromFront + 2,       # Case 3 
                   minFromFront + maxFromBack + 2)       # Case 4
class Solution {
    public int minimumDeletions(int[] nums) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int minInd = 0;
        int maxInd = 0;
        int n = nums.length;
        
        //First Find out the max and min element index
        for(int i=0;i<n;i++){
            if(nums[i]>max){
                max = nums[i];
                maxInd = i;
            }
            
            if(nums[i]<min){
                min = nums[i];
                minInd = i;
            }
        }
        
        //if both index are same then return the part in which less number of elements are there
        if(maxInd==minInd){
            return Math.min(maxInd+1,n-maxInd);
        }
        
        //max element is right side of min element
        if(maxInd>minInd){
            int count = Math.min(maxInd+1,n-minInd); // min of all the elements till max element and all the elements to the right of min element
            int count1 = minInd+1+(n-maxInd); // all elements to the left of min and right of max
            return Math.min(count,count1); // min of both
        }
        // min element is right side of the max element
        else{
            int count = Math.min(minInd+1,n-maxInd);
            int count1 = maxInd+1+(n-minInd);
            return Math.min(count,count1);
        }
        
    }
}
class Solution {
public:
    int minimumDeletions(vector<int>& nums) {

        int indmin, indmax, mini(INT_MAX), maxi(INT_MIN), n(nums.size());

        for (int i=0; i<n; i++)
        {
            if (maxi < nums[i]) maxi = nums[i], indmax = i;
            if (mini > nums[i]) mini = nums[i], indmin = i;
        }

        int before(min(indmin, indmax)), after(max(indmin, indmax));
        return min(before+1 + min(n-after, after-before), n-after + min(before+1, after-before));
    }
};
var minimumDeletions = function(nums) {
    if(nums.length===1)return 1
    let min=Math.min()
    let max=Math.max()
    
	// finding min and max
    for(let n of nums){
        min=Math.min(n,min)
        max=Math.max(n,max)
    };
    
	// obj to store no. of elements to remove if we start from left and if we start from right
    const obj={
        left:[],
        right:[]
    }
	// left[0]-> no. of elements upto min element if we start from left
	// left[1]-> no. of elements upto max element if we start from left
	// right[0]-> no. of elements upto min element if we start from right
	// right[1]-> no. of elements upto max element if we start from right
    for(let i=0;i<nums.length;i++){
        if(nums[i]===max){
            obj['left'][1]=i+1
            obj['right'][1]=nums.length-i
        }
        if(nums[i]===min){
            obj['left'][0]=i+1
            obj['right'][0]=nums.length-i
        }
    }
    let total=0
    const set = new Set();
	// if no. of elements to remove min element from left is less than right 
    if(obj['left'][0]<obj['right'][0]){
        set.add('left');
        total+=obj['left'][0]
    }else{
		// vice versa
        set.add('right');
        total+=obj['right'][0]
    }
	// if no. of elements to remove max element from right is less than left 
     if(obj['left'][1]<obj['right'][1]){
        set.add('left');
         total+=obj['left'][1]
    }else{
		// vice versa
        set.add('right');
        total+=obj['right'][1]
    }
	
	// if both elements are to be removed from diff direction inorder to remove lesser element
	// we have 3 cases: 
	// one from left and one from right(total), min/max element lies after max/min ele from left,min/max element lies after max/min ele from right
    if(set.size===2)return Math.min(total,Math.max(...obj['left']),Math.max(...obj['right']));
    else{
	   // if both elements can be removed from same direction(ie. either left or right)
        return Math.max(...obj[set.values().next().value])
    }
    
};
Removing Minimum and Maximum From Array