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++
#include <bits/stdc++.h>
using namespace std;
int minDeletions( int *arr, int N)
{
int mn = INT_MAX, mx = INT_MIN;
for ( int i = 0; i < N; i++) {
mn = min(mn, arr[i]);
mx = max(mx, arr[i]);
}
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;
int x = N - maxIndex + minIndex + 1;
int y = N - minIndex;
int z = maxIndex + 1;
return min({x, y, z});
}
int main()
{
int N = 6;
int arr[] = {2, -1, 9, 7, -2, 3};
cout << minDeletions(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int minDeletions( int arr[], int N)
{
int mn = Integer.MAX_VALUE, mx = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
mn = Math.min(mn, arr[i]);
mx = Math.max(mx, arr[i]);
}
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;
int x = N - maxIndx + minIndx + 1 ;
int y = N - minIndx;
int z = maxIndx + 1 ;
return Math.min(x, Math.min(y, z));
}
public static void main(String[] args)
{
int arr[] = { 2 , - 1 , 9 , 7 , - 2 , 3 };
int N = 6 ;
System.out.println(minDeletions(arr, N));
}
}
|
Python3
INT_MIN = - 2147483648
INT_MAX = 2147483647
def minDeletions(arr, N):
mn = INT_MAX
mx = INT_MIN
for i in range ( 0 , N):
mn = min (mn, arr[i])
mx = max (mx, arr[i])
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
x = N - maxIndex + minIndex + 1
y = N - minIndex
z = maxIndex + 1
return min ({x, y, z})
if __name__ = = "__main__" :
N = 6
arr = [ 2 , - 1 , 9 , 7 , - 2 , 3 ]
print (minDeletions(arr, N))
|
C#
using System;
class GFG
{
static int minDeletions( int [] arr, int N)
{
int mn = int .MaxValue, mx = int .MinValue;
for ( int i = 0; i < N; i++) {
mn = Math.Min(mn, arr[i]);
mx = Math.Max(mx, arr[i]);
}
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;
int x = N - maxIndx + minIndx + 1;
int y = N - minIndx;
int z = maxIndx + 1;
return Math.Min(x, Math.Min(y, z));
}
public static void Main()
{
int [] arr = { 2, -1, 9, 7, -2, 3 };
int N = 6;
Console.Write(minDeletions(arr, N));
}
}
|
Javascript
<script>
function minDeletions(arr, N)
{
let mn = Number.MAX_SAFE_INTEGER,
mx = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < N; i++) {
mn = Math.min(mn, arr[i]);
mx = Math.max(mx, arr[i]);
}
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;
let x = N - maxIndex + minIndex + 1;
let y = N - minIndex;
let z = maxIndex + 1;
return Math.min(x, Math.min(y, z));
}
let N = 6;
let arr = [2, -1, 9, 7, -2, 3];
document.write(minDeletions(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
|