Ctrl K

Best Sum Without Repeats

Easy
You are given an integer array values. You may remove any number of its elements, but at least one must stay. After the removal, all remaining numbers must be **pairwise distinct**. Return the largest possible sum of the remaining numbers.
Example 1
values=[3, -2, 3, 5, 0] → 8

Remove -2 and one of the 3s, leaving [3, 5, 0]: no repeats and the sum is 8.

Example 2
values=[-7, -4, -4] → -4

Every number is negative and the array may not become empty, so the best choice is to keep a single -4.

Example 3
values=[10, -1, 2, 10, 2] → 12
Constraints
  • 1 ≤ len(values) ≤ 100
  • -100 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsClearing the Stacks Evenly EasyJobs With a Cooldown MediumNarrowest Range Touching Every List HardSorted Into Runs of Three MediumFewest Guests by Lanyard Color Medium
loading Python…
Input
values=[3, -2, 3, 5, 0]
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.