Open In App

Remove minimum elements from either side such that 2*min becomes more than max

Given an unsorted array, trim the array such that twice of minimum is greater than maximum in the trimmed array. Elements should be removed either end of the array.
Number of removals should be minimum.

Examples: 

arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200}
Output: 4
We need to remove 4 elements (4, 5, 100, 200)
so that 2*min becomes more than max.


arr[] = {4, 7, 5, 6}
Output: 0
We don't need to remove any element as 
4*2 > 7 (Note that min = 4, max = 8)

arr[] = {20, 7, 5, 6}
Output: 1
We need to remove 20 so that 2*min becomes
more than max

arr[] = {20, 4, 1, 3}
Output: 3
We need to remove any three elements from ends
like 20, 4, 1 or 4, 1, 3 or 20, 3, 1 or 20, 4, 1

We strongly recommend that you click here and practice it, before moving on to the solution.

Naive Solution: 

A naive solution is to try every possible case using recurrence. Following is the naive recursive algorithm. Note that the algorithm only returns minimum numbers of removals to be made, it doesn’t print the trimmed array. It can be easily modified to print the trimmed array as well. 

// Returns minimum number of removals to be made in
// arr[l..h]
minRemovals(int arr[], int l, int h)
1) Find min and max in arr[l..h]
2) If 2*min > max, then return 0.
3) Else return minimum of "minRemovals(arr, l+1, h) + 1"
   and "minRemovals(arr, l, h-1) + 1"

Following is the implementation of above algorithm. 




// C++ implementation of above approach
#include <iostream>
using namespace std;
  
// A utility function to find minimum of two numbers
int min(int a, int b) {return (a < b)? a : b;}
  
// A utility function to find minimum in arr[l..h]
int min(int arr[], int l, int h)
{
    int mn = arr[l];
    for (int i=l+1; i<=h; i++)
       if (mn > arr[i])
         mn = arr[i];
    return mn;
}
  
// A utility function to find maximum in arr[l..h]
int max(int arr[], int l, int h)
{
    int mx = arr[l];
    for (int i=l+1; i<=h; i++)
       if (mx < arr[i])
         mx = arr[i];
    return mx;
}
  
// Returns the minimum number of removals from either end
// in arr[l..h] so that 2*min becomes greater than max.
int minRemovals(int arr[], int l, int h)
{
    // If there is 1 or less elements, return 0
    // For a single element, 2*min > max 
    // (Assumption: All elements are positive in arr[])
    if (l >= h) return 0;
  
    // 1) Find minimum and maximum in arr[l..h]
    int mn = min(arr, l, h);
    int mx = max(arr, l, h);
  
    //If the property is followed, no removals needed
    if (2*mn > mx)
       return 0;
  
    // Otherwise remove a character from left end and recur,
    // then remove a character from right end and recur, take
    // the minimum of two is returned
    return min(minRemovals(arr, l+1, h),
               minRemovals(arr, l, h-1)) + 1;
}
  
// Driver program to test above functions
int main()
{
  int arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200};
  int n = sizeof(arr)/sizeof(arr[0]);
  cout << minRemovals(arr, 0, n-1);
  return 0;
}




// Java implementation of above approach
import java.io.*;
import java.util.*;
  
class GFG {
    // A utility function to find minimum of two numbers
    static int min(int a, int b) { return (a < b) ? a : b; }
  
    // A utility function to find minimum in arr[l..h]
    static int min(int arr[], int l, int h)
    {
        int mn = arr[l];
        for (int i = l + 1; i <= h; i++)
            if (mn > arr[i])
                mn = arr[i];
        return mn;
    }
  
    // A utility function to find maximum in arr[l..h]
    static int max(int arr[], int l, int h)
    {
        int mx = arr[l];
        for (int i = l + 1; i <= h; i++)
            if (mx < arr[i])
                mx = arr[i];
        return mx;
    }
  
    // Returns the minimum number of removals from either
    // end in arr[l..h] so that 2*min becomes greater than
    // max.
    static int minRemovals(int arr[], int l, int h)
    {
        // If there is 1 or less elements, return 0
        // For a single element, 2*min > max
        // (Assumption: All elements are positive in arr[])
        if (l >= h)
            return 0;
  
        // 1) Find minimum and maximum in arr[l..h]
        int mn = min(arr, l, h);
        int mx = max(arr, l, h);
  
        // If the property is followed, no removals needed
        if (2 * mn > mx)
            return 0;
  
        // Otherwise remove a character from left end and
        // recur, then remove a character from right end and
        // recur, take the minimum of two is returned
        return min(minRemovals(arr, l + 1, h),
                   minRemovals(arr, l, h - 1))
            + 1;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 4, 5, 100, 9, 10, 11, 12, 15, 200 };
        int n = arr.length;
        System.out.print(minRemovals(arr, 0, n - 1));
    }
}
  
// This code is contributed by Mukul Singh.




# Python3 implementation of above approach
# A utility function to find 
# minimum in arr[l..h] 
def mini(arr, l, h): 
    mn = arr[l]
    for i in range(l + 1, h + 1):
        if (mn > arr[i]):
            mn = arr[i] 
    return mn
  
# A utility function to find 
# maximum in arr[l..h] 
def max(arr, l, h): 
    mx = arr[l] 
    for i in range(l + 1, h + 1):
        if (mx < arr[i]):
            mx = arr[i]
    return mx
  
  
# Returns the minimum number of 
# removals from either end in 
# arr[l..h] so that 2*min becomes 
# greater than max. 
def minRemovals(arr, l, h):
      
    # If there is 1 or less elements, return 0 
    # For a single element, 2*min > max 
    # (Assumption: All elements are positive in arr[]) 
    if (l >= h):
        return 0
  
    # 1) Find minimum and maximum 
    #    in arr[l..h] 
    mn = mini(arr, l, h) 
    mx = max(arr, l, h)
  
    # If the property is followed,
    # no removals needed 
    if (2 * mn > mx):
        return 0
  
    # Otherwise remove a character from 
    # left end and recur, then remove a 
    # character from right end and recur, 
    # take the minimum of two is returned 
    return (min(minRemovals(arr, l + 1, h), 
                minRemovals(arr, l, h - 1)) + 1)
  
# Driver Code
arr = [4, 5, 100, 9, 10,
       11, 12, 15, 200
n = len(arr)
print(minRemovals(arr, 0, n - 1)) 
  
# This code is contributed 
# by sahilshelangia




// C# implementation of above approach
using System;
  
class GFG
{
// A utility function to find minimum of two numbers
static int min(int a, int b) {return (a < b)? a : b;}
  
// A utility function to find minimum in arr[l..h]
static int min(int[] arr, int l, int h)
{
    int mn = arr[l];
    for (int i=l+1; i<=h; i++)
    if (mn > arr[i])
        mn = arr[i];
    return mn;
}
  
// A utility function to find maximum in arr[l..h]
static int max(int[] arr, int l, int h)
{
    int mx = arr[l];
    for (int i=l+1; i<=h; i++)
    if (mx < arr[i])
        mx = arr[i];
    return mx;
}
  
// Returns the minimum number of removals from either end
// in arr[l..h] so that 2*min becomes greater than max.
static int minRemovals(int[] arr, int l, int h)
{
    // If there is 1 or less elements, return 0
    // For a single element, 2*min > max 
    // (Assumption: All elements are positive in arr[])
    if (l >= h) return 0;
  
    // 1) Find minimum and maximum in arr[l..h]
    int mn = min(arr, l, h);
    int mx = max(arr, l, h);
  
    //If the property is followed, no removals needed
    if (2*mn > mx)
    return 0;
  
    // Otherwise remove a character from left end and recur,
    // then remove a character from right end and recur, take
    // the minimum of two is returned
    return min(minRemovals(arr, l+1, h),
            minRemovals(arr, l, h-1)) + 1;
}
  
// Driver Code
public static void Main()
{
int[] arr = {4, 5, 100, 9, 10, 11, 12, 15, 200};
int n = arr.Length;
Console.Write(minRemovals(arr, 0, n-1));
}
}
  
// This code is contributed by Akanksha Rai




<?php
// PHP implementation of above approach
  
// A utility function to find 
// minimum in arr[l..h]
function min_1(&$arr, $l, $h)
{
    $mn = $arr[$l];
    for ($i = $l + 1; $i <= $h; $i++)
    if ($mn > $arr[$i])
        $mn = $arr[$i];
    return $mn;
}
  
// A utility function to find
// maximum in arr[l..h]
function max_1(&$arr, $l, $h)
{
    $mx = $arr[$l];
    for ($i = $l + 1; $i <= $h; $i++)
    if ($mx < $arr[$i])
        $mx = $arr[$i];
    return $mx;
}
  
// Returns the minimum number of removals 
// from either end in arr[l..h] so that
// 2*min becomes greater than max.
function minRemovals(&$arr, $l, $h)
{
    // If there is 1 or less elements, 
    // return 0. For a single element, 
    // 2*min > max. (Assumption: All 
    // elements are positive in arr[])
    if ($l >= $h) return 0;
  
    // 1) Find minimum and maximum in arr[l..h]
    $mn = min_1($arr, $l, $h);
    $mx = max_1($arr, $l, $h);
  
    // If the property is followed, 
    // no removals needed
    if (2 * $mn > $mx)
    return 0;
  
    // Otherwise remove a character from left
    // end and recur, then remove a character 
    // from right end and recur, take the 
    // minimum of two is returned
    return min(minRemovals($arr, $l + 1, $h),
               minRemovals($arr, $l, $h - 1)) + 1;
}
  
// Driver Code
$arr = array(4, 5, 100, 9, 10, 
             11, 12, 15, 200);
$n = sizeof($arr);
echo minRemovals($arr, 0, $n - 1);
  
// This code is contributed 
// by ChitraNayal
?>




<script>
      
    // A utility function to find minimum in arr[l..h]
    function min(arr,l,h)
    {
        let mn = arr[l];
        for (let i=l+1; i<=h; i++)
        {
            if (mn > arr[i])
                mn = arr[i];
        }
        return mn;
    }
      
    // A utility function to find maximum in arr[l..h]
    function max(arr,l,h)
    {
        let mx = arr[l];
        for (let i=l+1; i<=h; i++)
        {
            if (mx < arr[i])
                mx = arr[i];
        }
        return mx;
    }
      
    // Returns the minimum number of removals from either end
    // in arr[l..h] so that 2*min becomes greater than max.
    function minRemovals(arr,l,h)
    {
      
        // If there is 1 or less elements, return 0
        // For a single element, 2*min > max 
        // (Assumption: All elements are positive in arr[])
        if (l >= h)
            return 0;
          
        // 1) Find minimum and maximum in arr[l..h]
        let mn = min(arr, l, h);
        let mx = max(arr, l, h);
          
        // If the property is followed, no removals needed
        if (2*mn > mx)
            return 0;
          
        // Otherwise remove a character from left end and recur,
        // then remove a character from right end and recur, take
        // the minimum of two is returned
        return Math.min(minRemovals(arr, l+1, h),minRemovals(arr, l, h-1)) + 1;
    }
      
    // Driver Code
    let arr = [4, 5, 100, 9, 10, 11, 12, 15, 200];
    let n = arr.length;
    document.write(minRemovals(arr, 0, n - 1));
      
    // This code is contributed by rag2127
</script>

Output
4

Time complexity: Time complexity of the above function can be written as following

  T(n) = 2T(n-1) + O(n) 

An upper bound on solution of above recurrence would be O(n x 2n).
Auxiliary Space: O(1)

Dynamic Programming: 

The above recursive code exhibits many overlapping subproblems. For example minRemovals(arr, l+1, h-1) is evaluated twice. So Dynamic Programming is the choice to optimize the solution. Following is Dynamic Programming based solution. 




// C++ program of above approach
#include <iostream>
using namespace std;
  
// A utility function to find minimum of two numbers
int min(int a, int b) {return (a < b)? a : b;}
  
// A utility function to find minimum in arr[l..h]
int min(int arr[], int l, int h)
{
    int mn = arr[l];
    for (int i=l+1; i<=h; i++)
       if (mn > arr[i])
         mn = arr[i];
    return mn;
}
  
// A utility function to find maximum in arr[l..h]
int max(int arr[], int l, int h)
{
    int mx = arr[l];
    for (int i=l+1; i<=h; i++)
       if (mx < arr[i])
         mx = arr[i];
    return mx;
}
  
// Returns the minimum number of removals from either end
// in arr[l..h] so that 2*min becomes greater than max.
int minRemovalsDP(int arr[], int n)
{
    // Create a table to store solutions of subproblems
    int table[n][n], gap, i, j, mn, mx;
  
    // Fill table using above recursive formula. Note that the table
    // is filled in diagonal fashion ,
    // from diagonal elements to table[0][n-1] which is the result.
    for (gap = 0; gap < n; ++gap)
    {
        for (i = 0, j = gap; j < n; ++i, ++j)
        {
            mn = min(arr, i, j);
            mx = max(arr, i, j);
            table[i][j] = (2*mn > mx)? 0: min(table[i][j-1]+1,
                                              table[i+1][j]+1);
        }
    }
    return table[0][n-1];
}
  
// Driver program to test above functions
int main()
{
  int arr[] = {20, 4, 1, 3};
  int n = sizeof(arr)/sizeof(arr[0]);
  cout << minRemovalsDP(arr, n);
  return 0;
}




// Java program of above approach
import java.util.*;
import java.io.*;
  
class GFG {
  
// A utility function to find minimum of two numbers
    static int min(int a, int b) {
        return (a < b) ? a : b;
    }
  
// A utility function to find minimum in arr[l..h]
    static int min(int arr[], int l, int h) {
        int mn = arr[l];
        for (int i = l + 1; i <= h; i++) {
            if (mn > arr[i]) {
                mn = arr[i];
            }
        }
        return mn;
    }
  
// A utility function to find maximum in arr[l..h]
    static int max(int arr[], int l, int h) {
        int mx = arr[l];
        for (int i = l + 1; i <= h; i++) {
            if (mx < arr[i]) {
                mx = arr[i];
            }
        }
        return mx;
    }
  
// Returns the minimum number of removals from either end
// in arr[l..h] so that 2*min becomes greater than max.
    static int minRemovalsDP(int arr[], int n) {
        // Create a table to store solutions of subproblems
        int table[][] = new int[n][n], gap, i, j, mn, mx;
  
        // Fill table using above recursive formula. Note that the table
        // is filled in diagonal fashion (similar to http://goo.gl/PQqoS),
        // from diagonal elements to table[0][n-1] which is the result.
        for (gap = 0; gap < n; ++gap) {
            for (i = 0, j = gap; j < n; ++i, ++j) {
                mn = min(arr, i, j);
                mx = max(arr, i, j);
                table[i][j] = (2 * mn > mx) ? 0 : min(table[i][j - 1] + 1,
                        table[i + 1][j] + 1);
            }
        }
        return table[0][n - 1];
    }
  
// Driver program to test above functions
    public static void main(String[] args) {
        int arr[] = {20, 4, 1, 3};
        int n = arr.length;
        System.out.println(minRemovalsDP(arr, n));
  
    }
}
// This code contributed by 29AJayKumar




# Python3 program of above approach
  
# A utility function to find 
# minimum in arr[l..h]
def min1(arr, l, h):
    mn = arr[l];
    for i in range(l + 1,h+1):
        if (mn > arr[i]):
            mn = arr[i];
    return mn;
  
# A utility function to find 
# maximum in arr[l..h]
def max1(arr, l, h):
    mx = arr[l];
    for i in range(l + 1, h + 1):
        if (mx < arr[i]):
            mx = arr[i];
    return mx;
  
# Returns the minimum number of removals
# from either end in arr[l..h] so that 
# 2*min becomes greater than max.
def minRemovalsDP(arr, n):
      
    # Create a table to store
    # solutions of subproblems
    table = [[0 for x in range(n)] for y in range(n)];
      
    # Fill table using above recursive formula.
    # Note that the table is filled in diagonal fashion
    # (similar to http:#goo.gl/PQqoS), from diagonal elements
    # to table[0][n-1] which is the result.
    for gap in range(n):
        i = 0;
        for j in range(gap,n):
            mn = min1(arr, i, j);
            mx = max1(arr, i, j);
            table[i][j] = 0 if (2 * mn > mx) else min(table[i][j - 1] + 1,table[i + 1][j] + 1);
            i += 1;
    return table[0][n - 1];
  
# Driver Code
arr = [20, 4, 1, 3];
n = len(arr);
print(minRemovalsDP(arr, n));
  
# This code is contributed by mits




// C# program of above approach
using System;
  
public class GFG {
   
    // A utility function to find minimum of two numbers
    static int min(int a, int b) {
        return (a < b) ? a : b;
    }
   
    // A utility function to find minimum in arr[l..h]
    static int min(int []arr, int l, int h) {
        int mn = arr[l];
        for (int i = l + 1; i <= h; i++) {
            if (mn > arr[i]) {
                mn = arr[i];
            }
        }
        return mn;
    }
   
// A utility function to find maximum in arr[l..h]
    static int max(int []arr, int l, int h) {
        int mx = arr[l];
        for (int i = l + 1; i <= h; i++) {
            if (mx < arr[i]) {
                mx = arr[i];
            }
        }
        return mx;
    }
   
    // Returns the minimum number of removals from either end
    // in arr[l..h] so that 2*min becomes greater than max.
    static int minRemovalsDP(int []arr, int n) {
        // Create a table to store solutions of subproblems
        int [,]table = new int[n,n];
        int gap, i, j, mn, mx;
   
        // Fill table using above recursive formula. Note that the table
        // is filled in diagonal fashion (similar to http://goo.gl/PQqoS),
        // from diagonal elements to table[0][n-1] which is the result.
        for (gap = 0; gap < n; ++gap) {
            for (i = 0, j = gap; j < n; ++i, ++j) {
                mn = min(arr, i, j);
                mx = max(arr, i, j);
                table[i,j] = (2 * mn > mx) ? 0 : min(table[i,j - 1] + 1,
                        table[i + 1,j] + 1);
            }
        }
        return table[0,n - 1];
    }
   
    // Driver program to test above functions
    public static void Main() {
        int []arr = {20, 4, 1, 3};
        int n = arr.Length;
        Console.WriteLine(minRemovalsDP(arr, n));
   
    }
}
// This code contributed by 29AJayKumar




<?php
// PHP program of above approach
  
// A utility function to find 
// minimum in arr[l..h]
function min1($arr, $l, $h)
{
    $mn = $arr[$l];
    for ($i = $l + 1; $i <= $h; $i++)
    if ($mn > $arr[$i])
        $mn = $arr[$i];
    return $mn;
}
  
// A utility function to find 
// maximum in arr[l..h]
function max1($arr, $l, $h)
{
    $mx = $arr[$l];
    for ($i = $l + 1; $i <= $h; $i++)
    if ($mx < $arr[$i])
        $mx = $arr[$i];
    return $mx;
}
  
// Returns the minimum number of removals
// from either end in arr[l..h] so that 
// 2*min becomes greater than max.
function minRemovalsDP($arr, $n)
{
      
    // Create a table to store 
    // solutions of subproblems
    $table = array_fill(0, $n
             array_fill(0, $n, 0));
  
    // Fill table using above recursive formula. 
    // Note that the table is filled in diagonal fashion 
    // (similar to http://goo.gl/PQqoS), from diagonal elements 
    // to table[0][n-1] which is the result.
    for ($gap = 0; $gap < $n; ++$gap)
    {
        for ($i = 0, $j = $gap; $j < $n; ++$i, ++$j)
        {
            $mn = min1($arr, $i, $j);
            $mx = max1($arr, $i, $j);
            $table[$i][$j] = (2 * $mn > $mx) ? 0 : 
                              min($table[$i][$j - 1] + 1,
                                  $table[$i + 1][$j] + 1);
        }
    }
    return $table[0][$n - 1];
}
  
// Driver Code
$arr = array(20, 4, 1, 3);
$n = count($arr);
echo minRemovalsDP($arr, $n);
  
// This code is contributed by mits
?>




<script>
  
// Javascript program of above approach    
  
// A utility function to find minimum 
// of two numbers
function minq(a, b)
{
    return (a < b) ? a : b;
}
  
// A utility function to find minimum
// in arr[l..h]
function min(arr, l, h) 
{
    var mn = arr[l];
    for(i = l + 1; i <= h; i++)
    {
        if (mn > arr[i]) 
        {
            mn = arr[i];
        }
    }
    return parseInt(mn);
}
  
// A utility function to find maximum in arr[l..h]
function max(arr, l, h) 
{
    var mx = arr[l];
    for(i = l + 1; i <= h; i++)
    {
        if (mx < arr[i])
        {
            mx = arr[i];
        }
    }
    return parseInt(mx);
}
  
// Returns the minimum number of removals
// from either end in arr[l..h] so that
// 2*min becomes greater than max.
function minRemovalsDP(arr, n)
{
      
    // Create a table to store solutions 
    // of subproblems
    var table = Array(n);
    var gap, i, j, mn, mx;
      
    for(i = 0; i < n; i++)
        table[i] = Array(n).fill(0);
  
    // Fill table using above recursive formula.
    // Note that the table is filled in diagonal 
    // fashion (similar to http://goo.gl/PQqoS),
    // from diagonal elements to table[0][n-1] 
    // which is the result.
    for(gap = 0; gap < n; ++gap)
    {
        for(i = 0, j = gap; j < n; ++i, ++j) 
        {
            mn = min(arr, i, j);
            mx = max(arr, i, j);
            table[i][j] = parseInt((2 * mn > mx) ? 
                  0 : minq(table[i][j - 1] + 1,
                           table[i + 1][j] + 1));
        }
    }
    return table[0][n - 1];
}
  
// Driver code
var arr = [ 20, 4, 1, 3 ];
var n = arr.length;
  
document.write(minRemovalsDP(arr, n));
  
// This code is contributed by gauravrajput1 
  
</script>

Output
3

Time Complexity: O(n3) where n is the number of elements in arr[]. 
Auxiliary Space: O(n^2)

Further Optimizations: 

The above code can be optimized in many ways. 

  1. We can avoid calculation of min() and/or max() when min and/or max is/are not changed by removing corner elements.
  2. We can pre-process the array and build segment tree in O(n) time. After the segment tree is built, we can query range minimum and maximum in O(Logn) time. The overall time complexity is reduced to O(n2Logn) time.

A O(n^2) Solution:

The idea is to find the maximum sized subarray such that 2*min > max. We run two nested loops, the outer loop chooses a starting point and the inner loop chooses ending point for the current starting point. We keep track of longest subarray with the given property.

Following is the implementation of the above approach. Thanks to Richard Zhang for suggesting this solution. 




// A O(n*n) solution to find the minimum of elements to
// be removed
#include <iostream>
#include <climits>
using namespace std;
  
// Returns the minimum number of removals from either end
// in arr[l..h] so that 2*min becomes greater than max.
int minRemovalsDP(int arr[], int n)
{
    // Initialize starting and ending indexes of the maximum
    // sized subarray with property 2*min > max
    int longest_start = -1, longest_end = 0;
  
    // Choose different elements as starting point
    for (int start=0; start<n; start++)
    {
        // Initialize min and max for the current start
        int min = INT_MAX, max = INT_MIN;
  
        // Choose different ending points for current start
        for (int end = start; end < n; end ++)
        {
            // Update min and max if necessary
            int val = arr[end];
            if (val < min) min = val;
            if (val > max) max = val;
  
            // If the property is violated, then no
            // point to continue for a bigger array
            if (2 * min <= max) break;
  
            // Update longest_start and longest_end if needed
            if (end - start > longest_end - longest_start ||
                longest_start == -1)
            {
                longest_start = start;
                longest_end = end;
            }
        }
    }
  
    // If not even a single element follow the property,
    // then return n
    if (longest_start == -1) return n;
  
    // Return the number of elements to be removed
    return (n - (longest_end - longest_start + 1));
}
  
// Driver program to test above functions
int main()
{
    int arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << minRemovalsDP(arr, n);
    return 0;
}




// A O(n*n) solution to find the minimum of elements to 
// be removed 
import java.util.*;
import java.io.*;
  
class GFG {
  
// Returns the minimum number of removals from either end 
// in arr[l..h] so that 2*min becomes greater than max. 
    static int minRemovalsDP(int arr[], int n) {
        // Initialize starting and ending indexes of the maximum 
        // sized subarray with property 2*min > max 
        int longest_start = -1, longest_end = 0;
  
        // Choose different elements as starting point 
        for (int start = 0; start < n; start++) {
            // Initialize min and max for the current start 
            int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
  
            // Choose different ending points for current start 
            for (int end = start; end < n; end++) {
                // Update min and max if necessary 
                int val = arr[end];
                if (val < min) {
                    min = val;
                }
                if (val > max) {
                    max = val;
                }
  
                // If the property is violated, then no 
                // point to continue for a bigger array 
                if (2 * min <= max) {
                    break;
                }
  
                // Update longest_start and longest_end if needed 
                if (end - start > longest_end - longest_start
                        || longest_start == -1) {
                    longest_start = start;
                    longest_end = end;
                }
            }
        }
  
        // If not even a single element follow the property, 
        // then return n 
        if (longest_start == -1) {
            return n;
        }
  
        // Return the number of elements to be removed 
        return (n - (longest_end - longest_start + 1));
    }
  
// Driver program to test above functions 
    public static void main(String[] args) {
        int arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200};
        int n = arr.length;
        System.out.println(minRemovalsDP(arr, n));
    }
}
  
// This code is contributed by PrinciRaj1992 




# A O(n*n) solution to find the minimum of
# elements to be removed
import sys;
  
# Returns the minimum number of removals 
# from either end in arr[l..h] so that 
# 2*min becomes greater than max.
def minRemovalsDP(arr, n):
  
    # Initialize starting and ending indexes 
    # of the maximum sized subarray 
    # with property 2*min > max
    longest_start = -1;
    longest_end = 0;
  
    # Choose different elements as starting point
    for start in range(n):
          
        # Initialize min and max 
        # for the current start
        min = sys.maxsize;
        max = -sys.maxsize;
  
        # Choose different ending points for current start
        for end in range(start,n):
            # Update min and max if necessary
            val = arr[end];
            if (val < min):
                min = val;
            if (val > max):
                max = val;
  
            # If the property is violated, then no
            # point to continue for a bigger array
            if (2 * min <= max):
                break;
  
            # Update longest_start and longest_end if needed
            if (end - start > longest_end - longest_start or longest_start == -1):
                longest_start = start;
                longest_end = end;
  
    # If not even a single element follow the property,
    # then return n
    if (longest_start == -1):
        return n;
  
    # Return the number of elements to be removed
    return (n - (longest_end - longest_start + 1));
  
# Driver Code
arr = [4, 5, 100, 9, 10, 11, 12, 15, 200];
n = len(arr);
print(minRemovalsDP(arr, n));
  
# This code is contributed by mits




// A O(n*n) solution to find the minimum of elements to 
// be removed 
  
using System; 
public class GFG {
   
// Returns the minimum number of removals from either end 
// in arr[l..h] so that 2*min becomes greater than max. 
    static int minRemovalsDP(int []arr, int n) {
        // Initialize starting and ending indexes of the maximum 
        // sized subarray with property 2*min > max 
        int longest_start = -1, longest_end = 0;
   
        // Choose different elements as starting point 
        for (int start = 0; start < n; start++) {
            // Initialize min and max for the current start 
            int min = int.MaxValue, max = int.MinValue;
   
            // Choose different ending points for current start 
            for (int end = start; end < n; end++) {
                // Update min and max if necessary 
                int val = arr[end];
                if (val < min) {
                    min = val;
                }
                if (val > max) {
                    max = val;
                }
   
                // If the property is violated, then no 
                // point to continue for a bigger array 
                if (2 * min <= max) {
                    break;
                }
   
                // Update longest_start and longest_end if needed 
                if (end - start > longest_end - longest_start
                        || longest_start == -1) {
                    longest_start = start;
                    longest_end = end;
                }
            }
        }
   
        // If not even a single element follow the property, 
        // then return n 
        if (longest_start == -1) {
            return n;
        }
   
        // Return the number of elements to be removed 
        return (n - (longest_end - longest_start + 1));
    }
   
// Driver program to test above functions 
    public static void Main() {
        int []arr = {4, 5, 100, 9, 10, 11, 12, 15, 200};
        int n = arr.Length;
        Console.WriteLine(minRemovalsDP(arr, n));
    }
}
   
// This code is contributed by Rajput-Ji




<?php
// A O(n*n) solution to find the minimum of
// elements to be removed
  
// Returns the minimum number of removals 
// from either end in arr[l..h] so that 
// 2*min becomes greater than max.
function minRemovalsDP($arr, $n)
{
    // Initialize starting and ending indexes 
    // of the maximum sized subarray 
    // with property 2*min > max
    $longest_start = -1;
    $longest_end = 0;
  
    // Choose different elements as starting point
    for ($start = 0; $start < $n; $start++)
    {
          
        // Initialize min and max 
        // for the current start
        $min = PHP_INT_MAX;
        $max = PHP_INT_MIN;
  
        // Choose different ending points for current start
        for ($end = $start; $end < $n; $end ++)
        {
            // Update min and max if necessary
            $val = $arr[$end];
            if ($val < $min) $min = $val;
            if ($val > $max) $max = $val;
  
            // If the property is violated, then no
            // point to continue for a bigger array
            if (2 * $min <= $max) break;
  
            // Update longest_start and longest_end if needed
            if ($end - $start > $longest_end - $longest_start ||
                $longest_start == -1)
            {
                $longest_start = $start;
                $longest_end = $end;
            }
        }
    }
  
    // If not even a single element follow the property,
    // then return n
    if ($longest_start == -1) return $n;
  
    // Return the number of elements to be removed
    return ($n - ($longest_end - $longest_start + 1));
}
  
// Driver Code
$arr = array(4, 5, 100, 9, 10, 11, 12, 15, 200);
$n = sizeof($arr);
echo minRemovalsDP($arr, $n);
  
// This code is contributed by jit_t
?>




<script>
  
// A O(n*n) solution to find the minimum of elements to
// be removed
  
    // Returns the minimum number of removals from either end
    // in arr[l..h] so that 2*min becomes greater than max.
    function minRemovalsDP(arr,n)
    {
        // Initialize starting and ending indexes of the maximum
        // sized subarray with property 2*min > max
        let longest_start = -1, longest_end = 0;
          
        // Choose different elements as starting point
        for (let start = 0; start < n; start++)
        {
            // Initialize min and max for the current start
            let  min = Number.MAX_VALUE, max = Number.MIN_VALUE;
              
            // Choose different ending points for current start
            for (let end = start; end < n; end++)
            {
                // Update min and max if necessary
                let val = arr[end];
                if (val < min) 
                {
                    min = val;
                }
                if (val > max)
                {
                    max = val;
                }
                // If the property is violated, then no
                // point to continue for a bigger array
                if (2 * min <= max)
                {
                    break;
                }
                  
                // Update longest_start and longest_end if needed
                if (end - start > longest_end - longest_start
                        || longest_start == -1)
                {
                    longest_start = start;
                    longest_end = end;
                }
                  
            }
        }
        // If not even a single element follow the property,
        // then return n
        if (longest_start == -1)
        {
            return n;
        }
        // Return the number of elements to be removed
        return (n - (longest_end - longest_start + 1));
    }
      
    // Driver program to test above functions
    let arr=[4, 5, 100, 9, 10, 11, 12, 15, 200];
    let n = arr.length;
    document.write(minRemovalsDP(arr, n));
      
      
    // This code is contributed by avanitrachhadiya2155
      
</script>

Output
4

Time Complexity: O(N2)
Auxiliary Space: O(1)


Article Tags :