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

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 <bits/stdc++.h>
using namespace std;
 
// Function to calculate minimum deletions to
// remove initial minimum and maximum
int 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 Code
int main()
{
    int N = 6;
    int arr[] = {2, -1, 9, 7, -2, 3};
 
    cout << minDeletions(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import 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 approach
INT_MIN = -2147483648
INT_MAX = 2147483647
 
# Function to calculate minimum deletions to
# remove initial minimum and maximum
def 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 Code
if __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 approach
using 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

<script>
// Javascript program for the above approach
 
// Function to calculate minimum deletions to
// remove initial minimum and maximum
function minDeletions(arr, N)
{
 
  // To store initial minimum and maximum
  let mn = Number.MAX_SAFE_INTEGER,
    mx = Number.MIN_SAFE_INTEGER;
 
  // Iterate and find min and max in arr[]
  for (let 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
  let minIndex, maxIndex;
  for (let i = 0; i < N; i++) {
    if (arr[i] == mn) minIndex = i;
    if (arr[i] == mx) maxIndex = i;
  }
 
  let temp = Math.max(minIndex, maxIndex);
  minIndex = Math.min(minIndex, maxIndex);
  maxIndex = temp;
 
  // Calculating all possible case of
  // deletion operations
  let x = N - maxIndex + minIndex + 1;
  let y = N - minIndex;
  let z = maxIndex + 1;
 
  // Return minimum among all the three cases
  return Math.min(x, Math.min(y, z));
}
 
// Driver Code
 
let N = 6;
let arr = [2, -1, 9, 7, -2, 3];
 
document.write(minDeletions(arr, N));
 
// This code is contributed by gfgking.
</script>

Output

4

Time Complexity: O(N)

Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Greedy

Related
Minimum adjacent swaps required to get Kth smallest number greater than given number Minimum adjacent swaps required to get Kth smallest number greater than given number
Maximize sum of ratios of N given fractions by incrementing numerator and denominators K times by 1 Maximize sum of ratios of N given fractions by incrementing numerator and denominators K times by 1
Maximum length of consecutive 1s or 0s after flipping at most K characters Maximum length of consecutive 1s or 0s after flipping at most K characters
Find Minimum Number Of Arrows Needed To Burst All Balloons Find Minimum Number Of Arrows Needed To Burst All Balloons
Minimize value of |A - X| + |B - Y| + |C - Z| such that X * Y = Z Minimize value of |A - X| + |B - Y| + |C - Z| such that X * Y = Z

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13