Minimum Removals to Balance Array
Updated
Problem statement
You are given an integer array arr.
The array is called balanced if:
- the largest element is at most twice the smallest element
You may perform the following changes:
- Remove any number of elements
- Modify at most one remaining element to any positive integer
Your task is to determine the minimum number of elements that must be removed so that the resulting array can be made balanced under these rules.
Function
minimumRemovalsToBalance(arr: int[]) → intExamples
Example 1
arr = [7, 4, 2, 3, 12, 9]return = 2An optimal sequence of operations is:
- Change the second element from
4to8. - Remove the third and fourth elements,
2and3.- The modified array is
[7, 8, 12, 9] 12is less than or equal to2 * 7.
- The modified array is
Example 2
arr = [4, 6, 2, 9, 8, 7, 3]return = 2An optimal sequence of operations is:
- Change the third element from
2to11 - Remove the first and seventh elements,
4and3.- The modified array is
[6, 11, 9, 8, 7] 11is less than or equal to2 * 6.
- The modified array is
More Salesforce Online Assessment questions
- Count Subarrays With Product Equal to KOA · Seen Nov 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
- Get Message StatusOA · Seen Nov 2026
- Minimum Unique-Character Segments After DeletionOA · Seen Sep 2026
- Run-Length String CompressionOA · Seen Sep 2026
- Maximum Requests in WindowOA · Seen Sep 2026
- Final Pod Counts After LogsOA · Seen Aug 2026
- Is This Date a Weekend?OA · Seen Aug 2026
- Maximum Barbell WeightOA · Seen Jul 2026
- Minimum Operations to ZeroOA · Seen Jul 2026
Similar questions at other companies
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Minimum Machines for TasksExpedia · OAExpedia interview questions
- Find Minimum Possible SizeMathWorks · OAMathWorks interview questions
- Minimum Days to Watch All MoviesAdobe · OAAdobe interview questions
- Efficient Janitor TripsRoblox · OARoblox interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Can Every Package Fit in a BoxIMC Trading · OAIMC Trading interview questions
- Minimum Tunnel Crossing TimeArcesium · OAArcesium interview questions
- Get Maximum PairsAmazon · OA