Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize removals in given Array to make max less than twice of min

  • Difficulty Level : Hard
  • Last Updated : 07 Jan, 2022

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++




// C++ program for above approach
#include <iostream>
using namespace std;
 
// Function to return minimum
int Min(int a, int b)
{
    if (a < b)
        return a;
    else
        return b;
}
 
// Function to return maximum
int Max(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}
 
// Function to find minimum removal operations.
int findMinRemovals(int arr[], int n)
{
    // Stores the length of the maximum
    // size subarray
    int max_range = 0;
 
    // To keep track of the minimum and
    // the maximum elements
    // in a subarray
    int minimum, maximum;
 
    // Traverse the array and consider
    // each element as a
    // starting point of a subarray
    for (int i = 0; i < n && n - i >
         max_range; i++) {
        // Reset the minimum and the
        // maximum elements
        minimum = maximum = arr[i];
 
        // Find the maximum size subarray
        // `arr[i…j]`
        for (int j = i; j < n; j++) {
            // Find the minimum and the
            // maximum elements in the current
            // subarray. We must do this in
            // constant time.
            minimum = Min(minimum, arr[j]);
            maximum = Max(maximum, arr[j]);
 
            // Discard the subarray if the
            // invariant is violated
            if (2 * minimum <= maximum) {
                break;
            }
 
            // Update `max_range` when a
            // bigger subarray is found
            max_range = Max(max_range, j - i + 1);
        }
    }
 
    // Return array size - length of
    // the maximum size subarray
    return n - max_range;
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 6, 21, 7, 5, 9, 12 };
    int N = sizeof(arr) / sizeof(int);
 
    int res = findMinRemovals(arr, N);
 
    // Print the result
    cout << res;
 
    return 0;
}

Java




// Java program for above approach
class GFG {
 
  // Function to return minimum
  static int Min(int a, int b) {
    if (a < b)
      return a;
    else
      return b;
  }
 
  // Function to return maximum
  static int Max(int a, int b) {
    if (a > b)
      return a;
    else
      return b;
  }
 
  // Function to find minimum removal operations.
  static int findMinRemovals(int arr[], int n)
  {
     
    // Stores the length of the maximum
    // size subarray
    int max_range = 0;
 
    // To keep track of the minimum and
    // the maximum elements
    // in a subarray
    int minimum, maximum;
 
    // Traverse the array and consider
    // each element as a
    // starting point of a subarray
    for (int i = 0; i < n && n - i > max_range; i++) {
      // Reset the minimum and the
      // maximum elements
      minimum = maximum = arr[i];
 
      // Find the maximum size subarray
      // `arr[i…j]`
      for (int j = i; j < n; j++) {
        // Find the minimum and the
        // maximum elements in the current
        // subarray. We must do this in
        // constant time.
        minimum = Min(minimum, arr[j]);
        maximum = Max(maximum, arr[j]);
 
        // Discard the subarray if the
        // invariant is violated
        if (2 * minimum <= maximum) {
          break;
        }
 
        // Update `max_range` when a
        // bigger subarray is found
        max_range = Max(max_range, j - i + 1);
      }
    }
 
    // Return array size - length of
    // the maximum size subarray
    return n - max_range;
  }
 
  // Driver Code
  public static void main(String args[]) {
    int arr[] = { 4, 6, 21, 7, 5, 9, 12 };
    int N = arr.length;
 
    int res = findMinRemovals(arr, N);
 
    // Print the result
    System.out.println(res);
  }
}
 
// This code is contributed by gfgking.

Python3




# python3 program for above approach
 
# Function to return minimum
def Min(a, b):
 
    if (a < b):
        return a
    else:
        return b
 
# Function to return maximum
def Max(a, b):
 
    if (a > b):
        return a
    else:
        return b
 
# Function to find minimum removal operations.
def findMinRemovals(arr, n):
 
    # Stores the length of the maximum
    # size subarray
    max_range = 0
     
    # To keep track of the minimum and
    # the maximum elements
    # in a subarray
 
    # Traverse the array and consider
    # each element as a
    # starting point of a subarray
    for i in range(0, n):
        if n - i <= max_range:
            break
             
        # Reset the minimum and the
        # maximum elements
        minimum = arr[i]
        maximum = arr[i]
 
        # Find the maximum size subarray
        # `arr[i…j]`
        for j in range(i, n):
            # Find the minimum and the
            # maximum elements in the current
            # subarray. We must do this in
            # constant time.
            minimum = Min(minimum, arr[j])
            maximum = Max(maximum, arr[j])
 
            # Discard the subarray if the
            # invariant is violated
            if (2 * minimum <= maximum):
                break
 
                # Update `max_range` when a
                # bigger subarray is found
            max_range = Max(max_range, j - i + 1)
 
        # Return array size - length of
        # the maximum size subarray
    return n - max_range
 
# Driver Code
if __name__ == "__main__":
 
    arr = [4, 6, 21, 7, 5, 9, 12]
    N = len(arr)
 
    res = findMinRemovals(arr, N)
 
    # Print the result
    print(res)
 
    # This code is contributed by rakeshsahni

C#




// C# program for above approach
using System;
 
class GFG {
 
    // Function to return minimum
    static int Min(int a, int b)
    {
        if (a < b)
            return a;
        else
            return b;
    }
 
    // Function to return maximum
    static int Max(int a, int b)
    {
        if (a > b)
            return a;
        else
            return b;
    }
 
    // Function to find minimum removal operations.
    static int findMinRemovals(int[] arr, int n)
    {
 
        // Stores the length of the maximum
        // size subarray
        int max_range = 0;
 
        // To keep track of the minimum and
        // the maximum elements
        // in a subarray
        int minimum, maximum;
 
        // Traverse the array and consider
        // each element as a
        // starting point of a subarray
        for (int i = 0; i < n && n - i > max_range; i++) {
            // Reset the minimum and the
            // maximum elements
            minimum = maximum = arr[i];
 
            // Find the maximum size subarray
            // `arr[i…j]`
            for (int j = i; j < n; j++) {
                // Find the minimum and the
                // maximum elements in the current
                // subarray. We must do this in
                // constant time.
                minimum = Min(minimum, arr[j]);
                maximum = Max(maximum, arr[j]);
 
                // Discard the subarray if the
                // invariant is violated
                if (2 * minimum <= maximum) {
                    break;
                }
 
                // Update `max_range` when a
                // bigger subarray is found
                max_range = Max(max_range, j - i + 1);
            }
        }
 
        // Return array size - length of
        // the maximum size subarray
        return n - max_range;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 4, 6, 21, 7, 5, 9, 12 };
        int N = arr.Length;
 
        int res = findMinRemovals(arr, N);
 
        // Print the result
        Console.WriteLine(res);
    }
}
 
// This code is contributes by ukasp.

Javascript




<script>
 
// JavaScript program for above approach
 
// Function to return minimum
function Min(a, b)
{
    if (a < b)
        return a;
    else
        return b;
}
 
// Function to return maximum
function Max(a, b)
{
    if (a > b)
        return a;
    else
        return b;
}
 
// Function to find minimum removal operations.
function findMinRemovals(arr, n)
{
     
    // Stores the length of the maximum
    // size subarray
    let max_range = 0;
 
    // To keep track of the minimum and
    // the maximum elements
    // in a subarray
    let minimum, maximum;
 
    // Traverse the array and consider
    // each element as a
    // starting point of a subarray
    for(let i = 0; i < n && n - i > max_range; i++)
    {
         
        // Reset the minimum and the
        // maximum elements
        minimum = maximum = arr[i];
 
        // Find the maximum size subarray
        // `arr[i…j]`
        for(let j = i; j < n; j++)
        {
             
            // Find the minimum and the
            // maximum elements in the current
            // subarray. We must do this in
            // constant time.
            minimum = Min(minimum, arr[j]);
            maximum = Max(maximum, arr[j]);
 
            // Discard the subarray if the
            // invariant is violated
            if (2 * minimum <= maximum)
            {
                break;
            }
 
            // Update `max_range` when a
            // bigger subarray is found
            max_range = Max(max_range, j - i + 1);
        }
    }
 
    // Return array size - length of
    // the maximum size subarray
    return n - max_range;
}
 
// Driver Code
let arr = [ 4, 6, 21, 7, 5, 9, 12 ];
let N = arr.length;
 
let res = findMinRemovals(arr, N);
 
// Print the result
document.write(res);
 
// This code is contributed by Potta Lokesh
 
</script>
Output
4

Time Complexity: O(N*N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!