Greatest Sum Divisible by Three
A medium-tier problem at 51% community acceptance, tagged with Array, Dynamic Programming, Greedy. Reported in interviews at DE Shaw and 0 others.
Greatest Sum Divisible by Three is a medium-difficulty problem that asks you to remove the minimum number of elements from an array so the remaining sum is divisible by three. It's a tricky greedy + modular arithmetic problem that shows up in assessments at firms like DE Shaw. The naive approach (try all subsets) fails instantly on larger inputs. The real solution hinges on understanding remainders modulo three and making optimal removal choices. If this problem hits your live OA and you blank on the modulo logic, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Greatest Sum Divisible by Three"
Greatest Sum Divisible by Three is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe key insight is that you don't need to track which elements to keep. Instead, work backwards from the remainder classes modulo three. Sort the array, then greedily remove the smallest elements whose remainders conflict with divisibility by three. Most candidates try dynamic programming first and overcomplicate it. The trap is treating this like a knapsack problem when it's actually about remainder arithmetic. You need to handle two cases: elements with remainder 1 mod 3, and elements with remainder 2 mod 3. Removing one element with remainder 1 decreases the sum's remainder by 1. Removing two elements with remainder 2 does the same. The greedy choice is always to remove the smallest elements. StealthCoder is your safety net if the remainder pattern doesn't click during the assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Greatest Sum Divisible by Three recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Greatest Sum Divisible by Three interview FAQ
Is this really a medium problem or is it harder?+
Acceptance rate sits around 51%, which means roughly half of candidates solve it. It's genuinely medium in difficulty. The trick is recognizing the modulo arithmetic pattern. Once you see it, the code is straightforward. Miss it, and you're stuck iterating through subsets.
Should I use dynamic programming or greedy?+
Greedy works best here. Sort the array, then remove the smallest elements that have unhelpful remainders mod three. DP is overkill and slower. The greedy choice (always remove the smallest conflicting elements) is optimal because sum only depends on remainder, not on which specific elements you keep.
What's the main trap candidates fall into?+
Overcomplicating via DP or trying to enumerate subsets. The real trap is not recognizing that the problem reduces to managing three remainder classes. Also, forgetting to handle both remainder 1 and remainder 2 cases when you need to remove two elements.
Do I need to sort the array?+
Yes. Sorting lets you identify and remove the smallest elements from each remainder class. When you need to remove k elements, always pick the k smallest ones. Sorting makes that selection O(n log n) instead of guessing or scanning multiple times.
How does this relate to the other topics listed?+
Array and Sorting are foundational here. Dynamic Programming is a red herring for many. Greedy is the actual algorithmic pattern. You can phrase it as DP (state = current index and remainder), but greedy removal is cleaner and faster. Understanding modular arithmetic is the real skill being tested.
Want the actual problem statement? View "Greatest Sum Divisible by Three" on LeetCode →