Skip to main content

Find the Score Difference in a Game - Solution & Explanation

MediumArraySimulation8 min read
Practice this problem

Problem Statement

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

There are exactly two 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 ith 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.

 

Example 1:

Input: nums = [1,2,3]

Output: 0

Explanation:​​​​​​​

  • Game 0: Since the points are odd, the second player becomes active and gains nums[0] = 1 point.
  • Game 1: No swap occurs. The second player gains nums[1] = 2 points.
  • Game 2: Since the points are odd, the first player becomes active and gains nums[2] = 3 points.
  • The score difference is 3 - 3 = 0.

Example 2:

Input: nums = [2,4,2,1,2,1]

Output: 4

Explanation:

  • Games 0 to 2: The first player gains 2 + 4 + 2 = 8 points.
  • Game 3: Since the points are odd, the second player is now active and gains nums[3] = 1 point.
  • Game 4: The second player gains nums[4] = 2 points.
  • Game 5: Since the points are odd, the players swap roles. Then, because this is the 6th game, the players swap again. The second player gains nums[5] = 1 point.
  • The score difference is 8 - 4 = 4.

Example 3:

Input: nums = [1]

Output: -1

Explanation:

  • Game 0: Since the points are odd, the second player is now active and gains nums[0] = 1 point.
  • The score difference is 0 - 1 = -1.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000

Approach Overview

Problem Overview: You are given an array representing moves or scoring events in a game. Each element affects the score of one of the players according to the rules. The task is to simulate the sequence and compute the final score difference between the two sides.

Approach 1: Direct Simulation (O(n) time, O(1) space)

The straightforward solution iterates through the array and updates scores exactly as the game rules describe. Maintain two variables representing each player's score or maintain a single running difference. For every element, apply the rule that determines which player gains points and update the totals accordingly. This approach works because the game outcome depends entirely on sequential processing of events, making a simple pass sufficient. Time complexity is O(n) since each move is processed once, and space complexity is O(1) because only a few counters are stored.

Approach 2: Running Difference Simulation (O(n) time, O(1) space)

Instead of storing two separate scores, track the score difference directly. When player A scores, add the value to a running variable; when player B scores, subtract it. This removes the need for multiple variables and simplifies the final calculation since the running value already represents the difference. The algorithm still performs a single pass through the array and applies constant-time updates for each element. Time complexity remains O(n), and space complexity stays O(1).

Approach 3: Structured Game Simulation (O(n) time, O(1) space)

If the game includes turn-based behavior or conditional scoring, you can explicitly simulate each round. Maintain a variable representing the current player and update scores depending on the move type. This method mirrors how the game would run in reality and is easy to extend when rules involve extra conditions such as streak bonuses or skipped turns. The algorithm still processes each event once using simulation logic with constant additional memory. Time complexity is O(n) and space complexity is O(1).

Recommended for interviews: The direct simulation approach is what most interviewers expect. The problem is fundamentally about carefully translating rules into code and iterating through the input once. Mentioning a brute-force interpretation helps demonstrate understanding of the scoring process, but the optimal answer is the single-pass simulation using an array traversal with constant space.

Solution

We use a variable k to represent the role of the current player. Initially k = 1, when k = 1 it means the first player is the active player, and when k = -1 it means the second player is the active player. For each game, we update the value of k according to the problem description, and add the score of the current game multiplied by k to the answer. Finally, we return the answer.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct SimulationO(n)O(1)General case where each array element represents a scoring event
Running Difference TrackingO(n)O(1)When only the final score difference is needed
Structured Game SimulationO(n)O(1)When turns or conditional scoring rules must be explicitly simulated

Video Solution

weekly contest 490 | leetcode 3847 | leetcode 3848 | leetcode 3849 | leetcode 3850 | DSA • Code With Vick • 287 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Find the Score Difference in a Game easy or hard?
This problem is typically categorized as Medium because the implementation is straightforward but requires careful handling of the game rules. The algorithm itself is efficient with O(n) time and O(1) space using a simulation approach.
Find the Score Difference in a Game Python/Java solution
Implement a simple loop that iterates through the array and updates a score difference variable. Python, Java, C++, Go, and TypeScript implementations all follow the same logic: read each element, apply the scoring rule, and update the running difference in O(1) time.
How to solve Find the Score Difference in a Game in O(n)?
Traverse the array and simulate the scoring rules for each element. Update either two score counters or a single running difference depending on which player gains points. Because every element is processed exactly once, the algorithm runs in O(n) time with constant extra memory.
What is the best approach for Find the Score Difference in a Game?
The best approach is a single-pass simulation over the array of moves. Track either both player scores or a running score difference and update it for each event. This method processes each element once, giving O(n) time complexity with O(1) extra space.
Is Find the Score Difference in a Game asked at Google/Amazon/Meta?
Problems involving game simulation and score tracking frequently appear in coding interviews at companies like Amazon, Google, and Meta. While the exact problem may vary, interviewers often test the ability to translate game rules into efficient array-based simulation code.
What data structure is used in Find the Score Difference in a Game?
The primary data structure is an array that represents the sequence of scoring events or moves. The algorithm performs a linear traversal and maintains a few integer variables to track scores or the score difference.
What is the time complexity of Find the Score Difference in a Game?
The optimal solution runs in O(n) time because it iterates through the array once and performs constant-time updates for each element. Space complexity is O(1) since only a few variables are required to store scores or the running difference.

Ready to solve this problem?

Practice Find the Score Difference in a Game with our built-in code editor and test cases.

Practice on FleetCode