Delete and Earn solution explained

Delete and Earn gives you an array of positive integers; picking a number x earns x points but deletes every copy of x - 1 and x + 1. Because taking one copy of x never hurts taking the other copies, the real choice is which distinct values to take, with the rule that no two chosen values may be consecutive. Add up each value's total (x times its count) and run the same take-or-skip DP used for House Robber over the values in order, in O(n + m) time where m is the largest value, or O(n log n) by sorting the distinct values.

By Published 8 min read

The rules, restated

You hold a list of positive integers. In one move you choose an element x, gain x points, and remove that element along with every element equal to x - 1 or x + 1. You repeat until the list is empty. Return the most points you can collect. Note that removing x - 1 and x + 1 does not remove the other copies of x itself; those stay and can be picked later.

Arrays made up for this page
ArrayAnswerOne best plan
[2, 2, 2, 3, 9, 10]16All three 2s (6) and the 10; the 3 and the 9 are given up
[5, 6, 6, 7, 1]13Both 6s and the 1, or 5, 7 and 1: a tie
[1, 1, 1, 2, 4, 5, 5, 5, 6]18The three 1s and the three 5s
[4]4Only one number
Arrays made up for this page. Scroll sideways on a small screen.

Why it is a good interview question

The statement talks about moves and deletions, which pulls people toward simulation. The skill being tested is the reduction: seeing that order of moves does not matter, that copies of a value travel together, and that what remains is a known one-dimensional DP. Candidates who have solved House Robber before often recognize it once they write the totals per value in a row. Interviewers then like to ask what happens when the values are huge, which separates the dense array version from the sorted-values version.

Step 1: collapse copies into totals

Suppose a best plan takes some copy of x. At that moment every x - 1 and x + 1 disappears, and nothing else can ever remove the remaining copies of x, so taking them all only adds points. The reverse also holds: if a plan takes no copy of x, it can treat x as absent. So each distinct value v is either taken in full, earning v times its count, or not at all. For [2, 2, 2, 3, 9, 10] the totals are 2 → 6, 3 → 3, 9 → 9 and 10 → 10.

The only constraint left is that two chosen values cannot differ by exactly 1, since taking one would have deleted the other. Any set of distinct values that obeys that rule can be carried out: pick them in any order and nothing chosen is ever removed.

Step 2: a baseline that checks every set

With d distinct values there are 2^d subsets. Keeping only those with no consecutive pair and summing their totals gives the answer. It is a useful check for small inputs and a clear statement of what the faster code must match.

def delete_and_earn_brute(nums):
    total = {}
    for x in nums:
        total[x] = total.get(x, 0) + x
    values = sorted(total)
    best = 0
    for mask in range(1 << len(values)):  # every subset of distinct values
        chosen = [values[i] for i in range(len(values)) if mask >> i & 1]
        if all(b - a != 1 for a, b in zip(chosen, chosen[1:])):
            best = max(best, sum(total[v] for v in chosen))
    return best
Every subset of distinct values. O(2^d · d) time.

Step 3: take or skip each value

Walk the values in increasing order and keep two numbers: the best total so far if the current value is taken, and the best if it is skipped. Taking v is allowed only on top of a total that skipped v - 1, so take becomes skip plus v's points. Skipping v leaves the better of the two earlier totals. A value that has no copies simply has 0 points, which makes the dense version easy to write.

def delete_and_earn(nums):
    if not nums:
        return 0
    top = max(nums)
    points = [0] * (top + 1)
    for x in nums:
        points[x] += x                    # taking a value once means taking every copy of it
    take, skip = 0, 0                     # best total so far with value v taken / not taken
    for v in range(top + 1):
        take, skip = skip + points[v], max(take, skip)
    return max(take, skip)
Dense DP over 0..max(nums). O(n + m) time and O(m) space for the points array.

If the largest value is a million but the array has three numbers, a list of a million slots is wasteful. Sorting the distinct values fixes that. The rule now depends on the gap: when v is exactly one more than the previous distinct value, taking it must build on the skip total; when there is a gap, the two values cannot clash, so take builds on the best total so far.

function deleteAndEarn(nums) {
  const total = new Map();
  for (const x of nums) total.set(x, (total.get(x) || 0) + x);
  const values = [...total.keys()].sort((a, b) => a - b);
  let take = 0, skip = 0, last = null;
  for (const v of values) {
    const best = Math.max(take, skip);
    // a value next to the last one can only join a total that skipped the last one
    take = (last !== null && v === last + 1 ? skip : best) + total.get(v);
    skip = best;
    last = v;
  }
  return Math.max(take, skip);
}
Sparse DP over sorted distinct values. O(n + d log d) time, O(d) space.

On [2, 2, 2, 3, 9, 10] the distinct values are 2, 3, 9, 10. After 2: take 6, skip 0. After 3, which is adjacent: take 0 + 3 = 3, skip 6. After 9, which follows a gap: take 6 + 9 = 15, skip 6. After 10, adjacent again: take 6 + 10 = 16, skip 15. The answer is the larger, 16.

Time and space

n numbers, d distinct values, m the largest value
ApproachTimeExtra space
Every subset of distinct valuesO(2^d · d)O(d)
Dense take-or-skip over 0..mO(n + m)O(m)
Sorted distinct valuesO(n + d log d)O(d)
n numbers, d distinct values, m the largest value. Scroll sideways on a small screen.

Which to present depends on the limits you are given. When values are capped at a few thousand or tens of thousands, the dense version is simplest and fast. When they can be very large, lead with the sorted version, or a counting pass that only visits values that appear.

Inputs to try

  • A single number.
  • Many copies of one value, where the answer is value times count.
  • A long run of consecutive values such as 1 through 6, where the plan alternates.
  • Values with gaps, like 4 and 9, which never block each other.
  • One huge value next to small ones, such as [1000000, 2, 3], which should not allocate a million slots in the sparse version.

Where people go wrong

  • Simulating picks with recursion over the whole array, which explodes on repeated values.
  • Greedily taking the largest number first: in [2, 3, 3, 3, 4] that takes the 4, which wipes out every 3, then the 2, for 6 points, while the three 3s alone are worth 9.
  • Forgetting that copies multiply: summing distinct values instead of value times count.
  • In the sparse version, treating every pair of neighbours in the sorted list as conflicting, even when there is a gap between them.
  • Indexing a dense array by value without making room for the maximum value itself.

A clean explanation

Fictional example

Reducing it to House Robber
Candidate
If I take one 5, I might as well take every 5, because only the 4s and 6s disappear. So I'll add up each value's points first.
Candidate
Then the only rule is that I can't take two values that differ by one. Laid out on a number line, that's House Robber: no two neighbours.
Interviewer
What if the values go up to a billion?
Candidate
Then I wouldn't index an array by value. I'd sort the distinct values and only treat a pair as neighbours when they differ by exactly one. That's n plus d log d.

Next ones to try

A DP over a grid instead of a line shows up in Minimum Falling Path Sum, where the state is a whole row instead of two numbers. Dungeon Game is a DP that must run backwards to work. Word Break II and Combination Sum are good companions if you want to practise when a DP answer is enough and when you have to list every solution.

Rehearsing with Natively

This is a question where the reduction matters more than the code, so practice saying it. Natively, running on a Mac or Windows PC, can take a screenshot of the problem during practice and offer an approach and code in its overlay, a handy check on whether you spotted the House Robber link. Talk through a mock attempt and the transcript it saves will show whether you justified taking every copy of a value. See how the AI interview assistant works.

Questions people ask

How do you solve Delete and Earn?

Add up the points for each value (value times count), then go through the values in order keeping two totals, one where the current value is taken and one where it is skipped. Taking v must build on a total that skipped v - 1.

Why is Delete and Earn the same as House Robber?

Once you take any copy of x you can take them all, so each distinct value is fully in or out. The only rule left is that two chosen values cannot be consecutive, which is House Robber over the number line.

What is the time complexity of Delete and Earn?

O(n + m) with an array indexed by value up to the maximum m, or O(n + d log d) if you sort the d distinct values, which is better when values are large.

Does picking x delete the other copies of x?

No. It deletes every x - 1 and x + 1, but the other copies of x stay and can be picked for more points.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP