Find Minimum Cost to Remove Array Elements
Reported by candidates from MathWorks's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The MathWorks OA reported in July 2026 hands you a removal game that looks greedy and isn't. You pick two of the first three elements, pay the max, and repeat until fewer than three remain. Most people grab the two smallest each time and get burned on a tricky leftover. This is an array problem with a DP backbone. If you blank in the live assessment, StealthCoder runs invisibly on your desktop and gives you the solution, but the idea below should get you most of the way there.
The problem
You are given an integer array nums. Remove all elements by repeatedly applying these rules: When at least three elements remain, choose any two of the first three elements and remove them. The operation costs the maximum of the two removed values. When fewer than three elements remain, remove all remaining elements in one operation. That operation costs the maximum remaining value. Return the minimum total cost required to remove every element. Function minCost(nums: int[]) → int Examples Example 1 nums = [3,1,4,2] return = 6 Remove 3 and 4 from the first three elements for a cost of 4. The remaining array is [1,2]; removing both costs 2. The minimum total is 6. Example 2 nums = [1,2,3] return = 4 Remove 2 and 3 for a cost of 3, then remove the remaining 1 for a cost of 1. Constraints 1 <= nums.length <= 1000 1 <= nums[i] <= 10^6
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that the state is small. After each operation, the remaining array is always some suffix plus at most one leftover element from the earlier prefix. So define dp[i][j] as the min cost when j is the index of the leftover element and i is the start of the untouched suffix. From there, pick two of {j, i, i+1}, pay their max, and the third becomes the new leftover. When fewer than three remain, pay the max of what's left. That's O(n^2) states with constant transitions, fine for n up to 1000. The pitfall is pure greedy. Removing the two smallest can strand a huge value that you pay for later, and example 1 shows it: paying 4 early beats alternatives. Also handle n of 1 or 2 directly as the max. Use memoization or a bottom-up table, and test with a big value sitting early in the array. StealthCoder is your hedge if the state definition won't come to you under the clock.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Find Minimum Cost to Remove Array Elements 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass MathWorks's OA.
MathWorks reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Minimum Cost to Remove Array Elements FAQ
What's the trick in the MathWorks Find Minimum Cost to Remove Array Elements problem?+
Don't go greedy. Track a DP state of one leftover index plus the start of the untouched suffix. Each step removes two of the first three, so the third becomes the new leftover. That keeps states at O(n^2) and transitions constant.
Why does picking the two smallest values fail?+
Cost is the max of the pair, so removing small values together wastes the chance to hide a large value inside a pair. A large element left behind gets paid for later anyway. Example 1, [3,1,4,2], shows paying 4 early is optimal.
What edge cases should I test?+
Test length 1 and 2, where you just return the max. Test length 3, like [1,2,3] giving 4. Test a large value at the front, all equal values, and a length that leaves exactly one or two elements at the end. Those catch most off-by-one errors in the final removal.
What complexity is acceptable here?+
With nums length up to 1000, O(n^2) is fine, roughly a million states with constant work each. Anything exponential will time out. Use a bottom-up table or memoized recursion, and watch recursion depth if you go top-down.
How do I prepare for this in 48 hours?+
Practice one or two DP problems where state is a suffix plus a carried element. Then write this one from scratch: define state, base cases for fewer than three elements, and the three pair choices. Run both examples by hand before submitting.