Remove minimum elements from either side such that 2*min becomes more than max - GeeksforGeeks
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:
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.
http://ideone.com/NaZX87
There is an O(n*log(n)) solution for this problem.
The main idea is based on the observation that you can not increase min or decrease max by adding more number, but you can only do it by cut off number.
Thus we can use a sliding window go through the entire array to keep track of the longest satisfied sub-array in O(n), and build a sparse table to query min and max in range(i, j) in O(1).
Since building a sparse table will take time O(n*log(n)), the entire algorithm will have the time complexity of O(n*log(n)).
O(N^3)
A naive solution is to try every possible case using recurrence. Following is the naive recursive algorithm.
Read full article from Remove minimum elements from either side such that 2*min becomes more than max - GeeksforGeeks
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.
O(N^2)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.
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; //exit early // 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));}http://ideone.com/NaZX87
There is an O(n*log(n)) solution for this problem.
The main idea is based on the observation that you can not increase min or decrease max by adding more number, but you can only do it by cut off number.
Thus we can use a sliding window go through the entire array to keep track of the longest satisfied sub-array in O(n), and build a sparse table to query min and max in range(i, j) in O(1).
Since building a sparse table will take time O(n*log(n)), the entire algorithm will have the time complexity of O(n*log(n)).
O(N^3)
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 (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];}
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.dp
A naive solution is to try every possible case using recurrence. Following is the naive recursive algorithm.
T(n) = 2T(n-1) + O(n)
An upper bound on solution of above recurrence would be O(n x 2n).
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"
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;}