Determine the Winner of a Bowling Game - Problem

You are given two 0-indexed integer arrays player1 and player2, representing the number of pins that player 1 and player 2 hit in a bowling game, respectively.

The bowling game consists of n turns, and the number of pins in each turn is exactly 10.

Assume a player hits xi pins in the ith turn. The value of the ith turn for the player is:

  • 2xi if the player hits 10 pins in either (i - 1)th or (i - 2)th turn.
  • Otherwise, it is xi.

The score of the player is the sum of the values of their n turns.

Return:

  • 1 if the score of player 1 is more than the score of player 2,
  • 2 if the score of player 2 is more than the score of player 1, and
  • 0 in case of a draw.

Input & Output

Example 1 — Basic Strike Bonus
$ Input: player1 = [4,10,7,9], player2 = [3,5,7,6]
Output: 1
💡 Note: Player1: 4 + 10 + 14 (7*2 due to strike) + 18 (9*2 due to strike) = 46. Player2: 3 + 5 + 7 + 6 = 21. Player1 wins, return 1.
Example 2 — Tie Game
$ Input: player1 = [3,5,7,6], player2 = [8,10,10,2]
Output: 2
💡 Note: Player1: 3 + 5 + 7 + 6 = 21. Player2: 8 + 10 + 20 (10*2 due to strike) + 4 (2*2 due to strike) = 42. Player2 wins, return 2.
Example 3 — Multiple Strikes
$ Input: player1 = [2,3], player2 = [4,1]
Output: 0
💡 Note: Player1: 2 + 3 = 5. Player2: 4 + 1 = 5. It's a tie, return 0.

Constraints

  • 1 ≤ player1.length, player2.length ≤ 1000
  • 0 ≤ player1[i], player2[i] ≤ 10

Visualization

Tap to expand
Bowling Game Winner Determination INPUT Player 1: 4 10 STRIKE! 7 9 i=0 i=1 i=2 i=3 Player 2: 3 5 7 6 n = 4 turns Max pins per turn: 10 Strike = 10 pins (2x bonus) ALGORITHM STEPS 1 Initialize Scores score1 = 0, score2 = 0 2 Loop Each Turn Check i-1 and i-2 for strike 3 Calculate Values If strike: 2*xi, else: xi 4 Compare Scores Return winner (1, 2, or 0) Score Calculation: P1: 4 + 10 + 2*7 + 2*9 = 4 + 10 + 14 + 18 = 46 P2: 3 + 5 + 7 + 6 (no strikes) = 21 46 > 21 --> Player 1 Wins! FINAL RESULT 1 Output: 1 Final Scores P1: 46 P2: 21 Player 1 Wins! Score: 46 vs 21 Key Insight: The 2x multiplier applies when a player scored a strike (10 pins) in the previous 1-2 turns. Single pass O(n): For each turn, check if turns i-1 or i-2 had 10 pins, then add 2*xi or xi accordingly. TutorialsPoint - Determine the Winner of a Bowling Game | Single Pass Calculation
Asked in
Amazon 15 Microsoft 8
12.4K Views
Medium Frequency
~15 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen