Remove minimum elements from either side such that 2*min becomes more than max | Set 2
Given an unsorted array, trim the array such that twice of minimum is greater than the maximum in the trimmed array. Elements should be removed from either end of the array. The number of removals should be minimum.
Examples:
Input: 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.Input: arr[] = {4, 7, 5, 6}
Output: 0
We don’t need to remove any element as
4*2 > 7Input: arr[] = {20, 7, 5, 6}
Output: 1
Approach: We have discussed various approaches to solve this problem in O(n3), O(n2 * logn), and O(n2) time in previous article. In this articles, we are going to discuss a O(n * logn) time solution using Sliding Window and Segment Tree concepts.
- Construct Segment Tree for RangeMinimumQuery and RangeMaximumQuery for the given input array.
- Take two pointers start and end, and initialize both to 0.
- While end is less than the length of the input array, do the following:
- Find min and max in the current window using Segment Trees constructed in step 1.
- Check if 2 * min ≤ max, if so then increment start pointer else update max valid length so far, if required
- Increment end
- length(arr[]) – maxValidLength is the required answer.
Below is the implementation of the above approach:
Java
// Java implementation of the approach public class GFG { // Function to return the minimum removals // required so that the array satisfy // the given condition public int removeMinElements(int[] a) { int n = a.length; RangeMinimumQuery rMimQ = new RangeMinimumQuery(); int[] minTree = rMimQ.createSegmentTree(a); RangeMaximumQuery rMaxQ = new RangeMaximumQuery(); int[] maxTree = rMaxQ.createSegmentTree(a); int start = 0, end = 0; // To store min and max in the current window int min, max; int maxValidLen = 0; while (end < n) { min = rMimQ.rangeMinimumQuery(minTree, start, end, n); max = rMaxQ.rangeMaximumQuery(maxTree, start, end, n); if (2 * min <= max) start++; else maxValidLen = Math.max(maxValidLen, end - start + 1); end++; } return n - maxValidLen; } class RangeMinimumQuery { // Creates a new segment tree from // the given input array public int[] createSegmentTree(int[] input) { int n = input.length; int segTreeSize = 2 * getNextPowerOfTwo(n) - 1; int[] segmentTree = new int[segTreeSize]; createSegmentTreeUtil(segmentTree, input, 0, n - 1, 0); return segmentTree; } private void createSegmentTreeUtil(int[] segmentTree, int[] input, int low, int high, int pos) { if (low == high) { // Its a leaf node segmentTree[pos] = input[low]; return; } // Construct left and right subtrees and then // update value for current node int mid = (low + high) / 2; createSegmentTreeUtil(segmentTree, input, low, mid, (2 * pos + 1)); createSegmentTreeUtil(segmentTree, input, mid + 1, high, (2 * pos + 2)); segmentTree[pos] = Math.min(segmentTree[2 * pos + 1], segmentTree[2 * pos + 2]); } public int rangeMinimumQuery(int[] segmentTree, int from, int to, int inputSize) { return rangeMinimumQueryUtil(segmentTree, 0, inputSize - 1, from, to, 0); } private int rangeMinimumQueryUtil(int[] segmentTree, int low, int high, int from, int to, int pos) { // Total overlap if (from <= low && to >= high) { return segmentTree[pos]; } // No overlap if (from > high || to < low) { return Integer.MAX_VALUE; } // Partial overlap int mid = (low + high) / 2; int left = rangeMinimumQueryUtil(segmentTree, low, mid, from, to, (2 * pos + 1)); int right = rangeMinimumQueryUtil(segmentTree, mid + 1, high, from, to, (2 * pos + 2)); return Math.min(left, right); } } class RangeMaximumQuery { // Creates a new segment tree from given input array public int[] createSegmentTree(int[] input) { int n = input.length; int segTreeSize = 2 * getNextPowerOfTwo(n) - 1; int[] segmentTree = new int[segTreeSize]; createSegmentTreeUtil(segmentTree, input, 0, n - 1, 0); return segmentTree; } private void createSegmentTreeUtil(int[] segmentTree, int[] input, int low, int high, int pos) { if (low == high) { // Its a leaf node segmentTree[pos] = input[low]; return; } // Construct left and right subtrees and then // update value for current node int mid = (low + high) / 2; createSegmentTreeUtil(segmentTree, input, low, mid, (2 * pos + 1)); createSegmentTreeUtil(segmentTree, input, mid + 1, high, (2 * pos + 2)); segmentTree[pos] = Math.max(segmentTree[2 * pos + 1], segmentTree[2 * pos + 2]); } public int rangeMaximumQuery(int[] segmentTree, int from, int to, int inputSize) { return rangeMaximumQueryUtil(segmentTree, 0, inputSize - 1, from, to, 0); } private int rangeMaximumQueryUtil(int[] segmentTree, int low, int high, int from, int to, int pos) { // Total overlap if (from <= low && to >= high) { return segmentTree[pos]; } // No overlap if (from > high || to < low) { return Integer.MIN_VALUE; } // Partial overlap int mid = (low + high) / 2; int left = rangeMaximumQueryUtil(segmentTree, low, mid, from, to, (2 * pos + 1)); int right = rangeMaximumQueryUtil(segmentTree, mid + 1, high, from, to, (2 * pos + 2)); return Math.max(left, right); } } // Function to return the minimum power of 2 // which is greater than n private int getNextPowerOfTwo(int n) { int logPart = (int)Math.ceil(Math.log(n) / Math.log(2)); return (int)Math.pow(2, logPart); } // Driver code public static void main(String[] args) { int[] a = { 4, 5, 100, 9, 10, 11, 12, 15, 200 }; GFG gfg = new GFG(); System.out.println(gfg.removeMinElements(a)); } } |
4
Recommended Posts:
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove minimum elements from the array such that 2*min becomes more than max
- Remove minimum elements from array so that max <= 2 * min
- Remove minimum number of elements such that no common element exist in both array
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Number of elements that can be seen from right side
- Count smaller elements on right side using Set in C++ STL
- Count smaller elements on right side
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Remove exactly one element from the array such that max - min is minimum
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Minimum operations required to remove an array
- Remove elements from the array which appear more than k times
- Remove elements that appear strictly less than k times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Narendra_Jha



