Minimize deletions from either end to remove Minimum and Maximum from Array
Last Updated :
29 Jun, 2022
Given array of integers arr[] of size N, the task is to find the count of minimum number of deletion operations to remove minimum and the maximum element from the array. The elements can only be deleted from either end of the array.
Examples:
Input: arr = [2, 10, 7, 5, 4, 1, 8, 6]
Output: 5
Explanation: The minimum element is 1 and the maximum element is 10. We can visualise the deletion operations as below:
[2, 10, 7, 5, 4, 1, 8, 6]
[2, 10, 7, 5, 4, 1, 8]
[2, 10, 7, 5, 4, 1]
[2, 10, 7, 5, 4]
[10, 7, 5, 4]
[7, 5, 4]
Total 5 deletion operations performed. There is no other sequence with less deletions in which the minimum and maximum can be deleted.
Input: arr = [56]
Output: 1
Explanation: Because the array only has one entry, it serves as both the lowest and maximum value. With a single delete, we can eliminate it.
Input: arr = [2, 5, 8, 3, 6, 4]
Output: 3
Explanation: The minimum element is 2 and the maximum element is 8. We can visualise the deletion operations as below:
[2, 5, 8, 3, 6, 4]
[5, 8, 3, 6, 4]
[8, 3, 6, 4]
[3, 6, 4]
Total 3 deletions are performed. It is the minimum possible number of deletions.
Approach: The above problem can be solved using below observation:
Suppose the max and min elements exists at index i and j, or vice versa, as shown below:
[ _ _ _ _ _ min/max _ _ _ _ _ _ max/min _ _ _ _ _ _ ]
<-- a --> (i) <--- b ---> (j) <---- c ---->
<-----------------------N ------------------------->
where,
- i, j: index of either max or min element of the array
- a: distance of the minimum (or maximum) element from starting
- b: distance between minimum and maximum element
- c: distance between maximum( or minimum) element from ending
- N: length of array
Now let’s look at different possible ways of deletion:
- For removing one from start and the other from end:
No. of deletion = (a + c) = ( (i + 1) + (n – j) )
- For removing both of them from starting of the array:
No. of deletion = (a + b) = (j + 1)
- For removing both of them from the end of the array:
No. of deletion = (b + c) = (n – i)
Using the above equations we can now easily get distances using the index of min and max element. The answer is minimum of these 3 cases
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDeletions(vector< int >& nums)
{
int n = nums.size();
int minindex
= min_element(nums.begin(), nums.end())
- nums.begin();
int maxindex
= max_element(nums.begin(), nums.end())
- nums.begin();
if (minindex > maxindex)
swap(minindex, maxindex);
int bothend = (minindex + 1) +
(n - maxindex);
int frontend = (maxindex + 1);
int backend = (n - minindex);
int ans = min(bothend,
min(frontend, backend));
return ans;
}
int main()
{
vector< int > arr{ 2, 10, 7, 5, 4, 1, 8, 6 };
cout << minDeletions(arr) << endl;
vector< int > arr2{ 56 };
cout << minDeletions(arr2);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG{
int minDeletions( int [] nums)
{
int n = nums.length;
int minindex = findIndex(nums,
Arrays.stream(nums).min().getAsInt());
int maxindex = findIndex(
nums, Arrays.stream(nums).max().getAsInt());
if (minindex > maxindex)
{
minindex = minindex + maxindex;
maxindex = minindex - maxindex;
minindex = minindex - maxindex;
}
int bothend = (minindex + 1 ) + (n - maxindex);
int frontend = (maxindex + 1 );
int backend = (n - minindex);
int ans = Math.min(
bothend, Math.min(frontend, backend));
return ans;
}
public static int findIndex( int arr[], int t)
{
int len = arr.length;
return IntStream.range( 0 , len)
.filter(i -> t == arr[i])
.findFirst()
.orElse(- 1 );
}
public static void main(String[] args)
{
int [] arr = { 2 , 10 , 7 , 5 , 4 , 1 , 8 , 6 };
System.out.print( new GFG().minDeletions(arr) + "\n" );
int []arr2 = { 56 };
System.out.print( new GFG().minDeletions(arr2));
}
}
|
Python3
def minDeletions(nums):
n = len (nums)
minindex = nums.index( min (nums))
maxindex = nums.index( max (nums))
if (minindex > maxindex):
minindex, maxindex = maxindex, minindex
bothend = (minindex + 1 ) + (n - maxindex)
frontend = (maxindex + 1 )
backend = (n - minindex)
ans = min (bothend,
min (frontend, backend))
return ans
if __name__ = = "__main__" :
arr = [ 2 , 10 , 7 , 5 , 4 , 1 , 8 , 6 ]
print (minDeletions(arr))
arr2 = [ 56 ]
print (minDeletions(arr2))
|
C#
using System;
using System.Linq;
public class GFG{
int minDeletions( int [] nums)
{
int n = nums.Length;
int minindex = findIndex(nums,
nums.Min());
int maxindex = findIndex(
nums, nums.Max());
if (minindex > maxindex)
{
minindex = minindex + maxindex;
maxindex = minindex - maxindex;
minindex = minindex - maxindex;
}
int bothend = (minindex + 1) + (n - maxindex);
int frontend = (maxindex + 1);
int backend = (n - minindex);
int ans = Math.Min(
bothend, Math.Min(frontend, backend));
return ans;
}
public static int findIndex( int []arr, int t)
{
int len = arr.Length;
return Array.IndexOf(arr, t);
}
public static void Main(String[] args)
{
int [] arr = { 2, 10, 7, 5, 4, 1, 8, 6 };
Console.Write( new GFG().minDeletions(arr) + "\n" );
int []arr2 = { 56 };
Console.Write( new GFG().minDeletions(arr2));
}
}
|
Javascript
<script>
const minDeletions = (nums) => {
let n = nums.length;
let minindex = nums.indexOf(Math.min(...nums));
let maxindex = nums.indexOf(Math.max(...nums));
if (minindex > maxindex) {
let temp = minindex;
minindex = maxindex;
maxindex = temp;
}
let bothend = (minindex + 1) + (n - maxindex);
let frontend = (maxindex + 1);
let backend = (n - minindex);
let ans = Math.min(bothend, Math.min(frontend, backend));
return ans;
}
let arr = [2, 10, 7, 5, 4, 1, 8, 6];
document.write(`${minDeletions(arr)}<br/>`);
let arr2 = [56];
document.write(minDeletions(arr2));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...