Delete and Earn
Problem
You are given an integer array nums. You repeatedly perform the following operation until the array is empty: pick any element with value v, gain v points, and remove that element; at the same time, every element equal to v - 1 and every element equal to v + 1 is removed from the array without earning anything. Return the maximum number of points you can earn.
numsholds up to about twenty thousand positive integers, each no larger than about ten thousand.
Examples
nums = [5, 6, 4]→10. Take 6 and 4 (removing the 5). Taking 5 first would destroy both neighbors and leave only 5 points.nums = [3, 3, 4, 4, 4, 5]→12. Take all three 4s (removing the 3s and the 5) for 12. Taking the 3s and the 5 gives only 11.nums = [7]→7. One element, one point.
Hints
- Once you take one copy of value
v, is there any reason not to take every other copy ofv? What does that tell you about the decision per value? - Group the points by value:
gain[v] = v * count(v). Which values conflict with each other? - This is the no-two-adjacent-picks DP (House Robber) over consecutive values instead of positions.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.