Minimum deletions from front or back required to remove maximum and minimum from Array

• Difficulty Level : Hard
• Last Updated : 26 Oct, 2021

Given an array arr[] consisting of integers. The task is to find minimum deletions required to remove the initial minimum and maximum element from arr[].
NOTE: Deletion can be performed either from the front or back of the array.

Examples:

Input: arr[] = {5, 7, 2, 4, 3}
Output: 3
Explanation: Initial minimum = 2, Initial maximum = 7
Deleting first 3 from arr[] updates arr[] to {2, 4, 3} which is free from Initial maximum and minimum elements.
Therefore 3 operations are required which is minimum possible.

Input: arr[] = {2, -1, 3, 5, 8, -7}
Output: 2

Approach: The given problem can be solved by using Greedy Approach. Follow the steps below to solve the given problem.

• Initialize two variables say mn, mx to store minimum and maximum elements respectively.
• Use two variables say minIndex, maxIndex to store the last occurrence of minimum and maximum elements.
• Iterate arr[] with i
• update mn = min(mn, arr[i])
• update mx = max(mx, arr[i])
• Use two variables say minIndex, maxIndex to store the last occurrence of minimum and maximum elements.
• Iterate arr[] with i
• if arr[i] = mn, then update minIndex = i
• if arr[i] = mx, then update maxIndex = i
• Calculate all the cases of deletion and store in variables x, y, z.
• Return minimum among x, y, z as the final answer.

Below is the implementation of the above approach.

C++

 // C++ program for the above approach#include using namespace std; // Function to calculate minimum deletions to// remove initial minimum and maximumint minDeletions(int *arr, int N){    // To store initial minimum and maximum    int mn = INT_MAX, mx = INT_MIN;     // Iterate and find min and max in arr[]    for(int i = 0; i < N; i++) {        mn = min(mn, arr[i]);        mx = max(mx, arr[i]);    }     // To store indices of last min and max    int minIndex, maxIndex;    for(int i = 0; i < N; i++) {        if(arr[i] == mn) minIndex = i;        if(arr[i] == mx) maxIndex = i;    }      int temp = max(minIndex, maxIndex);    minIndex = min(minIndex, maxIndex);    maxIndex = temp;     // Calculating all possible case of    // deletion operations    int x = N - maxIndex + minIndex + 1;    int y = N - minIndex;    int z = maxIndex + 1;     // Return minimum among all the three cases    return min({x, y, z});} // Driver Codeint main(){    int N = 6;    int arr[] = {2, -1, 9, 7, -2, 3};     cout << minDeletions(arr, N);     return 0;}

Java

 // Java program for the above approachimport java.io.*;import java.util.*;class GFG{       // Function to calculate minimum deletions to    // remove initial minimum and maximum    static int minDeletions(int arr[], int N)    {               // To store initial minimum and maximum        int mn = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;         // Iterate and find min and max in arr[]        for (int i = 0; i < N; i++) {            mn = Math.min(mn, arr[i]);            mx = Math.max(mx, arr[i]);        }         // To store indices of last min and max        int minIndx = 0, maxIndx = 0;        for (int i = 0; i < N; i++) {            if (arr[i] == mn)                minIndx = i;            if (arr[i] == mx)                maxIndx = i;        }         int temp = Math.max(minIndx, maxIndx);        minIndx = Math.min(minIndx, maxIndx);        maxIndx = temp;         // Calculating all possible case of        // deletion operations        int x = N - maxIndx + minIndx + 1;        int y = N - minIndx;        int z = maxIndx + 1;         // Return minimum among all the three cases        return Math.min(x, Math.min(y, z));    }     // Driver Code    public static void main(String[] args)    {        int arr[] = { 2, -1, 9, 7, -2, 3 };        int N = 6;        System.out.println(minDeletions(arr, N));    }} // This code is contributed by dwivediyash

Python3

 # python program for the above approachINT_MIN = -2147483648INT_MAX = 2147483647 # Function to calculate minimum deletions to# remove initial minimum and maximumdef minDeletions(arr, N):         # To store initial minimum and maximum    mn = INT_MAX    mx = INT_MIN     # Iterate and find min and max in arr[]    for i in range(0, N):        mn = min(mn, arr[i])        mx = max(mx, arr[i])         # To store indices of last min and max    minIndex = 0    maxIndex = 0    for i in range(0, N):        if(arr[i] == mn):            minIndex = i        if(arr[i] == mx):            maxIndex = i     temp = max(minIndex, maxIndex)    minIndex = min(minIndex, maxIndex)    maxIndex = temp     # Calculating all possible case of    # deletion operations    x = N - maxIndex + minIndex + 1    y = N - minIndex    z = maxIndex + 1     # Return minimum among all the three cases    return min({x, y, z}) # Driver Codeif __name__ == "__main__":     N = 6    arr = [2, -1, 9, 7, -2, 3]     print(minDeletions(arr, N))     # This code is contributed by rakeshsahni

C#

 // C# program for the above approachusing System;class GFG{       // Function to calculate minimum deletions to    // remove initial minimum and maximum    static int minDeletions(int[] arr, int N)    {               // To store initial minimum and maximum        int mn = int.MaxValue, mx = int.MinValue;         // Iterate and find min and max in arr[]        for (int i = 0; i < N; i++) {            mn = Math.Min(mn, arr[i]);            mx = Math.Max(mx, arr[i]);        }         // To store indices of last min and max        int minIndx = 0, maxIndx = 0;        for (int i = 0; i < N; i++) {            if (arr[i] == mn)                minIndx = i;            if (arr[i] == mx)                maxIndx = i;        }         int temp = Math.Max(minIndx, maxIndx);        minIndx = Math.Min(minIndx, maxIndx);        maxIndx = temp;         // Calculating all possible case of        // deletion operations        int x = N - maxIndx + minIndx + 1;        int y = N - minIndx;        int z = maxIndx + 1;         // Return minimum among all the three cases        return Math.Min(x, Math.Min(y, z));    }     // Driver Code    public static void Main()    {        int[] arr = { 2, -1, 9, 7, -2, 3 };        int N = 6;        Console.Write(minDeletions(arr, N));    }} // This code is contributed by gfgking

Javascript


Output
4

Time Complexity: O(N)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up