Remove minimum elements from the array such that 2 * min becomes more than max | CPP program to remove minimum elements from the array such that 2 * min becomes more than max ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Removal ( vector < int > v , int n ) { sort ( v . begin ( ) , v . end ( ) ) ; int ans = INT_MAX ; for ( vector < int > :: iterator i = v . begin ( ) ; i != v . end ( ) ; i ++ ) { vector < int > :: iterator j = upper_bound ( v . begin ( ) , v . end ( ) , ( 2 * ( * i ) ) ) ; ans = min ( ans , n - ( int ) ( j - i ) ) ; } return ans ; } int main ( ) { vector < int > a = { 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 } ; int n = a . size ( ) ; cout << Removal ( a , n ) ; return 0 ; }