Minimum Paid Swaps to Group Ones and Twos
Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Swaps involving a zero cost nothing, and that one line in the Infosys problem decides everything. Candidates reported it in September 2026. It looks like a swap-minimization puzzle until you see it's inversion counting in disguise. Strip the zeros, then count the 1-2 pairs that are out of order in each direction, and take the smaller count. It's one pass with two counters. No sorting, no simulation. If your mind goes blank mid-OA, StealthCoder runs invisibly on your desktop and can hand you the approach while the proctor sees nothing. The idea is small enough to hold in your head tonight anyway.
The problem
You are given an array values containing only 0, 1, and 2. You may swap adjacent elements. A swap between 1 and 2 costs one. A swap involving 0 costs zero. Rearrange the array so that all ones form one block and all twos form another block; the ones block may come before or after the twos block. For this exercise, assume zeros may be placed anywhere outside or between the two value blocks. Return the minimum total paid cost. Equivalently, after zeros are removed, the remaining sequence must be all ones followed by all twos, or all twos followed by all ones. Function minimumPaidSwaps(values: int[]) → long Examples Example 1 values = [1,2,1,2] return = 1 Swap the middle 2,1 pair once to obtain [1,1,2,2]. Example 2 values = [2,0,1,0,2,1] return = 1 Ignoring zeros gives [2,1,2,1]. One paid crossing suffices for the order twos before ones; zeros can move for free. Example 3 values = [0,1,0,1,2,0,2] return = 0 The nonzero sequence already has all ones before all twos. Constraints 1 <= values.length <= 10^5. Each value is 0, 1, or 2. The answer fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Zeros move for free, so ignore them. What's left is a sequence of 1s and 2s. Adjacent swaps needed to sort a sequence equal its inversion count. If the target is ones then twos, the cost is the number of pairs where a 2 sits before a 1. If the target is twos then ones, the cost is the number of pairs where a 1 sits before a 2. The answer is the min of the two. Scan left to right, tracking ones seen and twos seen. On a 1, add twos seen to the first total. On a 2, add ones seen to the second total. Use a 64-bit type, because 10^5 elements can produce billions of pairs. Pitfalls: simulating swaps is quadratic and will time out, and computing only one direction gives wrong answers. Check Example 1: the totals are 1 and 3, so the answer is 1. If you freeze live, StealthCoder is the hedge. It reads the statement and gives you the two-counter solution without appearing on the shared screen.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Minimum Paid Swaps to Group Ones and Twos 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Infosys's OA.
Infosys reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Paid Swaps to Group Ones and Twos FAQ
How hard is Minimum Paid Swaps to Group Ones and Twos really?+
Easy once you see it, medium if you don't. The wording pushes you toward simulating swaps, but the real task is counting out-of-order pairs after removing zeros. The code is about ten lines. The hard part is trusting that zeros really don't matter for the cost.
What's the trick to solving it fast?+
Drop the zeros, then count inversions in both directions in one pass. Keep a count of ones and twos seen so far. When you hit a 1, add the twos seen. When you hit a 2, add the ones seen. Return the smaller of the two totals.
Why do I need both orders instead of just ones before twos?+
The statement allows the ones block to come before or after the twos block. Each order has its own cost. Ones first costs the 2-before-1 pairs. Twos first costs the 1-before-2 pairs. Skipping one direction fails cases like Example 2, where the cheaper order is twos first.
Do I need a hash table for this problem?+
No. Two integer counters are enough, since only three values exist and zeros get skipped. The hash-table hint isn't needed. A running count per value does the job in O(n) time and O(1) extra space.
How do I prepare for this in 48 hours?+
Practice the inversion-counting idea on a small mixed array by hand, then code the single-pass version. Test Examples 1 to 3 and an all-zeros input. Use a long for the totals. Because the Infosys OA was reported in September 2026, expect similar swap-cost framing with a simple counting core.