Problem · Array

Score Difference

Learn this problem
MediumIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Two players take turns removing numbers from a sequence. Player 1 moves first.

On each turn:

  1. Remove the first number from the current sequence and add it to the current player's score.
  2. If the removed number is even, reverse the remaining sequence.

Continue until the sequence is empty. Return Player 1 score - Player 2 score.

Function

scoreDifference(n: int, numSeq: int[]) → int

Examples

Example 1

n = 5numSeq = [3, 6, 2, 3, 5]return = 1

The removed values are 3, 6, 5, 3, and 2. Player 1 scores 10 and Player 2 scores 9, so the difference is 1.

Constraints

  • 1 ≤ numSeq.length ≤ 2 × 10^5
  • -10^4 ≤ numSeq[i] ≤ 10^4
  • n = numSeq.length

More IBM problems

drafts saved locally
public int scoreDifference(int n, int[] numSeq) {
    // Write your code here
}
n5
numSeq[3, 6, 2, 3, 5]
expected1
checking account