Remove minimum elements from either side such that 2*min becomes more than max
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.
arr[] = {4, 7, 5, 6}
Output: 0
We don't need to remove any element as
4*2 > 7 (Note that min = 4, max = 8)
arr[] = {20, 7, 5, 6}
Output: 1
We need to remove 20 so that 2*min becomes
more than max
arr[] = {20, 4, 1, 3}
Output: 3
We need to remove any three elements from ends
like 20, 4, 1 or 4, 1, 3 or 20, 3, 1 or 20, 4, 1
publicclasssolution {publicminRemovalsDP(int arr[],int n) {int longest_start =-1, longest_end =0;// finding longest continous sub-array which has 2*min>maxfor (int start =0; start < n; start++) {int min =Integer.MAX_VALUE, max =Integer.MIN_VALUE;// the memorisation lies in using previous end's result to get min and max of current end's resultfor (int end = start; end < n; end++) { min =Math.min(min, arr[end]); max =Math.max(max, arr[end]);// If the property is violated, then no point to continue for a bigger arrayif (2* min <= max)break;if (end - start > longest_end - longest_start || longest_start ==-1) { longest_start = start; longest_end = end; } } }if (longest_start ==-1)return n;return (n - (longest_end - longest_start +1)); }}