# Remove minimum elements from the array such that 2*min becomes more than max

Given an array of size N. The task is to remove minimum elements from the array such that twice of minimum number is greater than the maximum number in the modified array. Print the minimum number of elements removed.

Examples:

Input: arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200}
Output: 4
Remove 4 elements (4, 5, 100, 200)
so that 2*min becomes more than max.

Input: arr[] = {4, 7, 5, 6}
Output: 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• sort the given array
• Traverse from left to right in the array and for each element chosen (let it be x) with the index i, find the upper_bound of (2*x). let that index be j.Then, update the our required answer by (n-j+i) if (n-j+i) is less than current value of our answer .

Below is the implementation of the above approach :

 `// CPP program to remove minimum elements from the ` `// array such that 2*min becomes more than max ` `#include ` `using` `namespace` `std; ` ` `  `// Function to remove minimum elements from the ` `// array such that 2*min becomes more than max ` `int` `Removal(vector<``int``> v, ``int` `n) ` `{ ` `    ``// Sort the array ` `    ``sort(v.begin(), v.end()); ` ` `  `    ``// To store the required answer ` `    ``int` `ans = INT_MAX; ` ` `  `    ``// Traverse from left to right ` `    ``for` `(vector<``int``>::iterator i = v.begin(); i != v.end();  ` `                                                     ``i++) { ` ` `  `        ``vector<``int``>::iterator j = upper_bound(v.begin(),  ` `                                    ``v.end(), (2 * (*i))); ` ` `  `        ``// Update the answer ` `        ``ans = min(ans, n - (``int``)(j - i)); ` `    ``} ` ` `  `    ``// Return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``> a = { 4, 5, 100, 9, 10, 11, 12, 15, 200 }; ` ` `  `    ``int` `n = a.size(); ` ` `  `    ``// Function call ` `    ``cout << Removal(a, n); ` ` `  `    ``return` `0; ` `} `

 `// Java program to remove minimum elements from the  ` `// array such that 2*min becomes more than max ` `import` `java.util.Arrays; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to calculate upper bound ` `    ``public` `static` `int` `upperBound(``int``[] array, ` `                                 ``int` `value) ` `    ``{ ` `        ``int` `low = ``0``; ` `        ``int` `high = array.length; ` `        ``while` `(low < high)  ` `        ``{ ` `            ``final` `int` `mid = (low + high) / ``2``; ` `            ``if` `(value >= array[mid])  ` `            ``{ ` `                ``low = mid + ``1``; ` `            ``}  ` `            ``else`  `            ``{ ` `                ``high = mid; ` `            ``} ` `        ``} ` `        ``return` `low; ` `    ``} ` ` `  `    ``// Function to remove minimum elements from the ` `    ``// array such that 2*min becomes more than max ` `    ``public` `static` `int` `Removal(``int``[] v, ``int` `n)  ` `    ``{ ` ` `  `        ``// Sort the array ` `        ``Arrays.sort(v); ` ` `  `        ``// To store the required answer ` `        ``int` `ans = Integer.MAX_VALUE; ` `        ``int` `k = ``0``; ` ` `  `        ``// Traverse from left to right ` `        ``for` `(``int` `i : v)  ` `        ``{ ` `            ``int` `j = upperBound(v, (``2` `* i)); ` ` `  `            ``// Update the answer ` `            ``ans = Math.min(ans, n - (j - k)); ` `            ``k++; ` `        ``} ` ` `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int``[] a = { ``4``, ``5``, ``100``, ``9``, ``10``,  ` `                    ``11``, ``12``, ``15``, ``200` `}; ` `        ``int` `n = a.length; ` ` `  `        ``// Function call ` `        ``System.out.println(Removal(a, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

 `// C# program to remove minimum elements  ` `// from the array such that 2*min becomes ` `// more than max ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to calculate upper bound ` `    ``public` `static` `int` `upperBound(``int``[] array, ` `                                 ``int` `value) ` `    ``{ ` `        ``int` `low = 0; ` `        ``int` `high = array.Length; ` `        ``while` `(low < high)  ` `        ``{ ` `            ``int` `mid = (low + high) / 2; ` `            ``if` `(value >= array[mid])  ` `            ``{ ` `                ``low = mid + 1; ` `            ``}  ` `            ``else` `            ``{ ` `                ``high = mid; ` `            ``} ` `        ``} ` `        ``return` `low; ` `    ``} ` ` `  `    ``// Function to remove minimum elements from the ` `    ``// array such that 2*min becomes more than max ` `    ``public` `static` `int` `Removal(``int``[] v, ``int` `n)  ` `    ``{ ` ` `  `        ``// Sort the array ` `        ``Array.Sort(v); ` ` `  `        ``// To store the required answer ` `        ``int` `ans = ``int``.MaxValue; ` `        ``int` `k = 0; ` ` `  `        ``// Traverse from left to right ` `        ``foreach` `(``int` `i ``in` `v)  ` `        ``{ ` `            ``int` `j = upperBound(v, (2 * i)); ` ` `  `            ``// Update the answer ` `            ``ans = Math.Min(ans, n - (j - k)); ` `            ``k++; ` `        ``} ` ` `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int``[] a = { 4, 5, 100, 9, 10,  ` `                    ``11, 12, 15, 200 }; ` `        ``int` `n = a.Length; ` ` `  `        ``// Function call ` `        ``Console.WriteLine(Removal(a, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output :

```4
```

Time complexity: O(NlogN)

Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

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 : sanjeev2552, Rajput-Ji

Article Tags :