LC 3634 - Minimum Removals to Balance Array
Approach We sort the array first, and then find where the k times of the current value will be and update the best solution. I think we can do two pointer, or early termination when the current removal is already larger then best. Code class Solution { public: int minRemoval(vector<int>& nums, int k) { int n = nums.size(); int minRemoval = n; sort(nums.begin(), nums.end()); for(int i = 0 ; i < n ; ++i) { long long maxVal = (long long)k * nums[i]; if(maxVal > INT_MAX) maxVal = INT_MAX; int idx = upper_bound(nums.begin(), nums.end(), maxVal) - nums.begin(); int removal = n - idx; minRemoval = min(minRemoval, removal+i); } return minRemoval; } };