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