[LeetCode] Find the Score Difference in a Game

3847. Find the Score Difference in a Game

You are given an integer array nums, where nums[i] represents the points scored in the i^th game.

There are exactlytwo players. Initially, the first player is active and the second player is inactive.

The following rules apply sequentially for each game i:

  • If nums[i] is odd, the active and inactive players swap roles.
  • In every 6th game (that is, game indices 5, 11, 17, ...), the active and inactive players swap roles.
  • The active player plays the i^th game and gains nums[i] points.

Return the score difference, defined as the first player’s total score minus the second player’s total score.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int scoreDifference(vector<int>& nums) {
int score[2]{0,0}, run = 0;
for(int i = 0, game = 1; i < nums.size(); i++, game++) {
if(nums[i] & 1) run = !run;
if(game % 6 == 0) run = !run;
score[run] += nums[i];
}
return score[0] - score[1];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/find-the-score-difference-in-a-game/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.