Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array receiver of length n and an integer k. n players are playing a ball-passing game.
You choose the starting player, i. The game proceeds as follows: player i passes the ball to player receiver[i], who then passes it to receiver[receiver[i]], and so on, for k passes in total. The game's score is the sum of the indices of the players who touched the ball, including repetitions, i.e. i + receiver[i] + receiver[receiver[i]] + ... + receiver(k)[i].
Return the maximum possible score.
Notes:
receiver may contain duplicates.receiver[i] may be equal to i.Example 1:
Input: receiver = [2,0,1], k = 4
Output: 6
Explanation:
Starting with player i = 2 the initial score is 2:
| Pass | Sender Index | Receiver Index | Score |
|---|---|---|---|
| 1 | 2 | 1 | 3 |
| 2 | 1 | 0 | 3 |
| 3 | 0 | 2 | 5 |
| 4 | 2 | 1 | 6 |
Example 2:
Input: receiver = [1,1,1,2,3], k = 3
Output: 10
Explanation:
Starting with player i = 4 the initial score is 4:
| Pass | Sender Index | Receiver Index | Score |
|---|---|---|---|
| 1 | 4 | 3 | 7 |
| 2 | 3 | 2 | 9 |
| 3 | 2 | 1 | 10 |
Constraints:
1 <= receiver.length == n <= 1050 <= receiver[i] <= n - 11 <= k <= 1010Problem summary: You are given an integer array receiver of length n and an integer k. n players are playing a ball-passing game. You choose the starting player, i. The game proceeds as follows: player i passes the ball to player receiver[i], who then passes it to receiver[receiver[i]], and so on, for k passes in total. The game's score is the sum of the indices of the players who touched the ball, including repetitions, i.e. i + receiver[i] + receiver[receiver[i]] + ... + receiver(k)[i]. Return the maximum possible score. Notes: receiver may contain duplicates. receiver[i] may be equal to i.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Bit Manipulation
[2,0,1] 4
[1,1,1,2,3] 3
jump-game-vi)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
class Solution {
public long getMaxFunctionValue(List<Integer> receiver, long k) {
int n = receiver.size(), m = 64 - Long.numberOfLeadingZeros(k);
int[][] f = new int[n][m];
long[][] g = new long[n][m];
for (int i = 0; i < n; ++i) {
f[i][0] = receiver.get(i);
g[i][0] = i;
}
for (int j = 1; j < m; ++j) {
for (int i = 0; i < n; ++i) {
f[i][j] = f[f[i][j - 1]][j - 1];
g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1];
}
}
long ans = 0;
for (int i = 0; i < n; ++i) {
int p = i;
long t = 0;
for (int j = 0; j < m; ++j) {
if ((k >> j & 1) == 1) {
t += g[p][j];
p = f[p][j];
}
}
ans = Math.max(ans, p + t);
}
return ans;
}
}
// Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
func getMaxFunctionValue(receiver []int, k int64) (ans int64) {
n, m := len(receiver), bits.Len(uint(k))
f := make([][]int, n)
g := make([][]int64, n)
for i := range f {
f[i] = make([]int, m)
g[i] = make([]int64, m)
f[i][0] = receiver[i]
g[i][0] = int64(i)
}
for j := 1; j < m; j++ {
for i := 0; i < n; i++ {
f[i][j] = f[f[i][j-1]][j-1]
g[i][j] = g[i][j-1] + g[f[i][j-1]][j-1]
}
}
for i := 0; i < n; i++ {
p := i
t := int64(0)
for j := 0; j < m; j++ {
if k>>j&1 == 1 {
t += g[p][j]
p = f[p][j]
}
}
ans = max(ans, t+int64(p))
}
return
}
# Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
class Solution:
def getMaxFunctionValue(self, receiver: List[int], k: int) -> int:
n, m = len(receiver), k.bit_length()
f = [[0] * m for _ in range(n)]
g = [[0] * m for _ in range(n)]
for i, x in enumerate(receiver):
f[i][0] = x
g[i][0] = i
for j in range(1, m):
for i in range(n):
f[i][j] = f[f[i][j - 1]][j - 1]
g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1]
ans = 0
for i in range(n):
p, t = i, 0
for j in range(m):
if k >> j & 1:
t += g[p][j]
p = f[p][j]
ans = max(ans, t + p)
return ans
// Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
// class Solution {
// public long getMaxFunctionValue(List<Integer> receiver, long k) {
// int n = receiver.size(), m = 64 - Long.numberOfLeadingZeros(k);
// int[][] f = new int[n][m];
// long[][] g = new long[n][m];
// for (int i = 0; i < n; ++i) {
// f[i][0] = receiver.get(i);
// g[i][0] = i;
// }
// for (int j = 1; j < m; ++j) {
// for (int i = 0; i < n; ++i) {
// f[i][j] = f[f[i][j - 1]][j - 1];
// g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1];
// }
// }
// long ans = 0;
// for (int i = 0; i < n; ++i) {
// int p = i;
// long t = 0;
// for (int j = 0; j < m; ++j) {
// if ((k >> j & 1) == 1) {
// t += g[p][j];
// p = f[p][j];
// }
// }
// ans = Math.max(ans, p + t);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2836: Maximize Value of Function in a Ball Passing Game
// class Solution {
// public long getMaxFunctionValue(List<Integer> receiver, long k) {
// int n = receiver.size(), m = 64 - Long.numberOfLeadingZeros(k);
// int[][] f = new int[n][m];
// long[][] g = new long[n][m];
// for (int i = 0; i < n; ++i) {
// f[i][0] = receiver.get(i);
// g[i][0] = i;
// }
// for (int j = 1; j < m; ++j) {
// for (int i = 0; i < n; ++i) {
// f[i][j] = f[f[i][j - 1]][j - 1];
// g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1];
// }
// }
// long ans = 0;
// for (int i = 0; i < n; ++i) {
// int p = i;
// long t = 0;
// for (int j = 0; j < m; ++j) {
// if ((k >> j & 1) == 1) {
// t += g[p][j];
// p = f[p][j];
// }
// }
// ans = Math.max(ans, p + t);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.