FastPrepMaximum Points by Deleting Elements

Maximum Points by Deleting Elements

IBM logoIBM● MediumINTERNOA
Learn

Problem statement

You are given an integer array elements. Repeat these operations until the array is empty:

  1. Select a remaining value v.
  2. Remove every occurrence of v and add their sum to your score.
  3. Remove every occurrence of v - 1 and v + 1 without scoring those values.

Return the maximum score that can be earned.

Function

maxPoints(elements: int[]) → long

Examples

Example 1

elements = [5,6,6,4,11]return = 27

Take 11 for 11 points, both copies of 6 for 12 points, and 4 for 4 points, totaling 27.

Example 2

elements = [3,4,2]return = 6

Choose 2 and 4. They do not conflict and contribute 2 + 4 = 6.

Constraints

  • 1 <= elements.length <= 10^5
  • 1 <= elements[i] <= 10^5

More IBM problems

See IBM hiring insights
public long maxPoints(int[] elements) {
  // write your code here
}
elements[5,6,6,4,11]
expected27
Checking account…