- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# C++ program to remove minimum elements from either side such that 2*min becomes more than max

The problem involves removing elements from any side of a list of integers in such a way that 2*min is greater than max.

vector<int> arr = {250, 10, 11, 12, 19, 200}; res = solve(arr);

We can use brute force approach. We can try all possible subarrays which satisfy and find the longest one in which 2*min > max condition holds. We can also use dynamic programming approach to try all possible subarray combinations that are overkill and not required.

## Example (Using Vector ADT)

Suppose we have an array such as “[250, 10, 11, 12, 19, 200]”. To get the optimal solution, we need to remove elements [250, 200] to make the array [10, 11, 12, 19] where min is 10 and max is 19. So 2*10 > 19. We removed 2 elements from the array, so the output is printed as 2.

Following is a C++ program that depicts how to remove minimum number of elements from an array such that twice of minimum value is greater than the maximum value −

#include <iostream> #include <vector> using namespace std; int solve(vector<int> arr) { int startIndex = 0, endIndex = 0; bool foundAnAnswer = false; for (int start=0; start<arr.size(); start++) { int min = INT32_MAX, max = INT32_MIN; for (int end=start; end<arr.size(); end++) { if (arr[end] < min) min = arr[end]; if (arr[end] > max) max = arr[end]; if (2*min <= max) break; if (end - start > endIndex - startIndex || !foundAnAnswer) { startIndex = start; endIndex = end; foundAnAnswer = true; } } } if (!foundAnAnswer) return arr.size(); return (arr.size() - (endIndex - startIndex + 1)); } int main() { vector<int> arr = {250, 10, 11, 12, 19, 200}; cout << solve(arr); return 0; }

## Output

2

## Example (Without using Vector ADT)

Following is a C++ program that depicts how to remove minimum number of elements from an array such that twice of minimum value is greater than the maximum value, but without using the Vector ADT −

#include <iostream> using namespace std; int min(int a, int b) {return (a < b)? a : b;} int min(int arr[], int low, int high) { int minimum = arr[low]; for (int i=low+1; i<=high; i++) if (minimum > arr[i]) minimum = arr[i]; return minimum; } int max(int arr[], int low, int high) { int maximum = arr[low]; for (int i=low+1; i<=high; i++) if (maximum < arr[i]) maximum = arr[i]; return maximum; } int minimum_removals(int arr[], int low, int high) { if (low >= high) return 0; int m1 = min(arr, low, high); int m2 = max(arr, low, high); if (2*m1 > m2) return 0; return min(minimum_removals(arr, low+1, high), minimum_removals(arr, low, high-1)) + 1; } int main() { int arr[] = {12, 45, 32, 88, 100}; int n = sizeof(arr)/sizeof(arr[0]); cout << minimum_removals(arr, 0, n-1); return 0; }

## Output

3

## Conclusion

Here we used the brute force approach to find the longest subarray. Other possible solutions might include checking every possible subarray by repeatedly popping elements from both sides and other approaches. Still, they are implementation heavy and less optimized. The time complexity here is O(n^2) as we have traversed all subarrays.

- Related Articles
- Minimum removals from array to make max – min
- C++ Program to remove Characters from a Numeric String Such That String Becomes Divisible by 8
- Minimum value of “max + min” in a subarray in C++
- Array elements that appear more than once in C?
- C program to find sum, max and min with Variadic functions
- Place k elements such that minimum distance is maximized in C++
- Program to find sum of differences between max and min elements from randomly selected k balls from n balls in Python
- Array elements that appear more than once?
- C# program to remove duplicate elements from a List
- Find minimum value to assign all array elements so that array product becomes greater in C++
- Program to fill Min-max game tree in Python
- Ways to place items in n^2 positions such that no row/column contains more than one in C++
- Convert min Heap to max Heap in C++
- Python – Test if elements of list are in Min/Max range from other list
- Min-Max Heaps