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.

Updated on: 10-Aug-2022

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements