Minimize removals in given Array to make max less than twice of min
Given an integer array arr[]. The task is to minimize the number of removals required such that the maximum element in arr[] becomes less than twice the minimum.
Examples
Input: arr[] = {4, 6, 21, 7, 5, 9, 12}
Output: The minimum number of removal operations is 4.
Explanation: The newly trimmed array is [7, 5, 9] where 9 is max and 5 is min; 9 < 2 × 5.
Input: arr[] = {14, 21, 62, 43, 39, 57}
Output: The minimum number of removal operations is 2.
Explanation: The newly trimmed array is [62, 43, 39, 57]
where 62 is max and 39 is min; 62 < 2 × 39.
Approach: This problem can be solved by using the Greedy Approach. The thought is very simple and effective, consider the problem as finding the largest subarray whose maximum element is less than twice the minimum rather than removing elements from the array. Follow the steps below to solve the given problem.
- Traverse the given array from left to right and consider every element as the starting point of the subarray.
- With the chosen starting index of the subarray, greedily choose the other end of the subarray as far as possible.
- Expand the subarray to the right, one element at a time, such that the newly added element satisfies the given condition.
- Follow the same process for all subarrays to find the maximum size subarray.
- The difference between the size of the subarray and the input array will be the minimum number of removals needed.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int Min( int a, int b)
{
if (a < b)
return a;
else
return b;
}
int Max( int a, int b)
{
if (a > b)
return a;
else
return b;
}
int findMinRemovals( int arr[], int n)
{
int max_range = 0;
int minimum, maximum;
for ( int i = 0; i < n && n - i >
max_range; i++) {
minimum = maximum = arr[i];
for ( int j = i; j < n; j++) {
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
if (2 * minimum <= maximum) {
break ;
}
max_range = Max(max_range, j - i + 1);
}
}
return n - max_range;
}
int main()
{
int arr[] = { 4, 6, 21, 7, 5, 9, 12 };
int N = sizeof (arr) / sizeof ( int );
int res = findMinRemovals(arr, N);
cout << res;
return 0;
}
|
Java
class GFG {
static int Min( int a, int b) {
if (a < b)
return a;
else
return b;
}
static int Max( int a, int b) {
if (a > b)
return a;
else
return b;
}
static int findMinRemovals( int arr[], int n)
{
int max_range = 0 ;
int minimum, maximum;
for ( int i = 0 ; i < n && n - i > max_range; i++) {
minimum = maximum = arr[i];
for ( int j = i; j < n; j++) {
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
if ( 2 * minimum <= maximum) {
break ;
}
max_range = Max(max_range, j - i + 1 );
}
}
return n - max_range;
}
public static void main(String args[]) {
int arr[] = { 4 , 6 , 21 , 7 , 5 , 9 , 12 };
int N = arr.length;
int res = findMinRemovals(arr, N);
System.out.println(res);
}
}
|
Python3
def Min (a, b):
if (a < b):
return a
else :
return b
def Max (a, b):
if (a > b):
return a
else :
return b
def findMinRemovals(arr, n):
max_range = 0
for i in range ( 0 , n):
if n - i < = max_range:
break
minimum = arr[i]
maximum = arr[i]
for j in range (i, n):
minimum = Min (minimum, arr[j])
maximum = Max (maximum, arr[j])
if ( 2 * minimum < = maximum):
break
max_range = Max (max_range, j - i + 1 )
return n - max_range
if __name__ = = "__main__" :
arr = [ 4 , 6 , 21 , 7 , 5 , 9 , 12 ]
N = len (arr)
res = findMinRemovals(arr, N)
print (res)
|
C#
using System;
class GFG {
static int Min( int a, int b)
{
if (a < b)
return a;
else
return b;
}
static int Max( int a, int b)
{
if (a > b)
return a;
else
return b;
}
static int findMinRemovals( int [] arr, int n)
{
int max_range = 0;
int minimum, maximum;
for ( int i = 0; i < n && n - i > max_range; i++) {
minimum = maximum = arr[i];
for ( int j = i; j < n; j++) {
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
if (2 * minimum <= maximum) {
break ;
}
max_range = Max(max_range, j - i + 1);
}
}
return n - max_range;
}
public static void Main()
{
int [] arr = { 4, 6, 21, 7, 5, 9, 12 };
int N = arr.Length;
int res = findMinRemovals(arr, N);
Console.WriteLine(res);
}
}
|
Javascript
<script>
function Min(a, b)
{
if (a < b)
return a;
else
return b;
}
function Max(a, b)
{
if (a > b)
return a;
else
return b;
}
function findMinRemovals(arr, n)
{
let max_range = 0;
let minimum, maximum;
for (let i = 0; i < n && n - i > max_range; i++)
{
minimum = maximum = arr[i];
for (let j = i; j < n; j++)
{
minimum = Min(minimum, arr[j]);
maximum = Max(maximum, arr[j]);
if (2 * minimum <= maximum)
{
break ;
}
max_range = Max(max_range, j - i + 1);
}
}
return n - max_range;
}
let arr = [ 4, 6, 21, 7, 5, 9, 12 ];
let N = arr.length;
let res = findMinRemovals(arr, N);
document.write(res);
</script>
|
Time Complexity: O(N*N)
Auxiliary Space: O(1)
Last Updated :
07 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...