Reported June 2026
Salesforcesliding window

Minimum Removals to Balance Array

Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Salesforce OA. Under 2s to a working solution.
Founder's read

Salesforce reported this one in June 2026, and the trap is hiding in the "modify at most one element" clause. Most people see "largest at most twice the smallest" and jump straight to sort plus sliding window. That gets you the no-modify answer and nothing more. If you've got an OA invite for Salesforce coming up, this is a sorted-array window problem with one twist that changes the count. Miss the twist and Example 1 comes out wrong. StealthCoder sits invisibly on your screen during the live OA as a safety net if you blank on the edge case, but the idea is simple enough to walk in knowing.

The problem

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[]) → int

Examples
Example 1
arr = [7, 4, 2, 3, 12, 9]
return = 2
An optimal sequence of operations is:
Change the second element from 4 to 8.
Remove the third and fourth elements, 2 and 3.The modified array is [7, 8, 12, 9]
12 is less than or equal to 2 * 7.
Example 2
arr = [4, 6, 2, 9, 8, 7, 3]
return = 2
An optimal sequence of operations is:
Change the third element from 2 to 11
Remove the first and seventh elements, 4 and 3.The modified array is [6, 11, 9, 8, 7]
11 is less than or equal to 2 * 6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Sort the array. Any kept set is a contiguous window in sorted order, so the base idea is the longest window where max <= 2 * min. The twist is the single modification. You can change one kept element to any positive integer, which means one outlier can be rewritten to sit inside the range. So a window can be extended by one element past the usual limit, either a too-large element on the right or a too-small one on the left, because you rewrite it into range. Try each left bound with two pointers and allow one skipped violator, then take the max kept size. Answer is n minus that. The pitfall is forgetting a modified element also removes itself as the min or max constraint, and arrays of size 1 or 2 are always fixable with zero removals. StealthCoder is the hedge if the live clock rattles you, but test those tiny cases yourself first.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Minimum Removals to Balance Array cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Removals to Balance Array FAQ

What's the trick in Minimum Removals to Balance Array?+

Sort first, then think in windows. The kept elements form a contiguous block in sorted order. The one allowed modification lets a single element outside the max <= 2 * min rule be rewritten into range, so each window can absorb one violator.

How hard is this Salesforce OA question really?+

Medium. The sort plus two pointers part is routine. The modification clause is what trips people up. If you can solve the plain version without modification, you're most of the way there and only need to handle the extra slot.

What edge cases break a naive solution?+

Arrays with one or two elements, which need zero removals since you can modify one to fit. Duplicates, and cases where the best fix is rewriting the smallest element instead of the largest. Check Example 1, where 4 becomes 8 to make it work.

What's the time complexity I should aim for?+

O(n log n) from the sort, then O(n) for the pointer sweep. Anything quadratic that tries every element as the modified one with a full rescan is likely fine for tiny inputs but risky if the constraints are large.

How do I prepare in 48 hours?+

Do the sliding window on sorted arrays pattern a few times, like longest subarray with a max-min constraint. Then hand-trace both examples with the modification rule. Write a brute force to compare against your solution on random small arrays.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Salesforce.

OA at Salesforce?
Invisible during screen share
Get it