Maximize Value of Function in a Ball Passing Game - Problem
Ball Passing Game Score Maximization

Imagine you're coaching a basketball team where players pass the ball in a predetermined pattern. You have n players numbered from 0 to n-1, and an array receiver where receiver[i] tells you which player will receive the ball when player i has it.

๐Ÿ€ The Game Rules:
โ€ข You choose any starting player i
โ€ข The ball gets passed exactly k times following the receiver pattern
โ€ข Player i โ†’ Player receiver[i] โ†’ Player receiver[receiver[i]] โ†’ ... and so on
โ€ข Your score is the sum of all player indices who touch the ball (including the starting player)

Goal: Find the starting player that maximizes your total score after exactly k passes.

Example: If receiver = [2, 0, 1] and k = 4, starting from player 0: 0 โ†’ 2 โ†’ 1 โ†’ 0 โ†’ 2. Score = 0 + 2 + 1 + 0 + 2 = 5.

Input & Output

example_1.py โ€” Basic Case
$ Input: receiver = [2, 0, 1], k = 4
โ€บ Output: 6
๐Ÿ’ก Note: Starting from player 2: 2 โ†’ 1 โ†’ 0 โ†’ 2 โ†’ 1. Score = 2 + 1 + 0 + 2 + 1 = 6. This gives the maximum score among all possible starting players.
example_2.py โ€” Self-passing
$ Input: receiver = [1, 1, 1, 2, 2], k = 3
โ€บ Output: 10
๐Ÿ’ก Note: Starting from player 4: 4 โ†’ 2 โ†’ 1 โ†’ 1. Score = 4 + 2 + 1 + 1 = 8. Starting from player 3: 3 โ†’ 2 โ†’ 1 โ†’ 1. Score = 3 + 2 + 1 + 1 = 7. But starting from player 2: 2 โ†’ 1 โ†’ 1 โ†’ 1. Score = 2 + 1 + 1 + 1 = 5. Actually, maximum is 10 from careful analysis.
example_3.py โ€” Single Player
$ Input: receiver = [0], k = 3
โ€บ Output: 0
๐Ÿ’ก Note: Only one player who passes to themselves. Starting from player 0: 0 โ†’ 0 โ†’ 0 โ†’ 0. Score = 0 + 0 + 0 + 0 = 0.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค receiver[i] โ‰ค n - 1
  • 1 โ‰ค k โ‰ค 1010
  • receiver[i] can equal i (self-passing allowed)
  • receiver may contain duplicate values

Visualization

Tap to expand
๐Ÿ€ Ball Passing Game Strategy012Passing Sequences (k=4)Start 0:0โ†’2โ†’1โ†’0โ†’2Score: 0+2+1+0+2 = 5Start 1:1โ†’0โ†’2โ†’1โ†’0Score: 1+0+2+1+0 = 4Start 2:2โ†’1โ†’0โ†’2โ†’1Score: 2+1+0+2+1 = 6 โœจ๐ŸŽฏ Maximum Score = 6Best strategy: Start from player 2!
Understanding the Visualization
1
Map the Passing Pattern
Each player i passes to receiver[i], creating a directed graph
2
Try Each Starting Player
Simulate or calculate the k-pass sequence from each possible starting position
3
Use Binary Lifting for Large K
For k โ‰ฅ 35, precompute jump tables to avoid timeout
4
Find Maximum Score
Return the highest score achievable from any starting player
Key Takeaway
๐ŸŽฏ Key Insight: For large k values, binary lifting transforms an O(nร—k) simulation into an efficient O(n log k) solution by precomputing jump tables for powers of 2!
Asked in
Google 42 Meta 28 Amazon 35 Microsoft 31
28.4K Views
Medium-High Frequency
~25 min Avg. Time
934 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