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.
Move from brute-force thinking to an efficient approach using array strategy.
Alice and Bob are opponents in an archery competition. The competition has set the following rules:
numArrows arrows and then Bob shoots numArrows arrows.0 to 11 inclusive.k (in between 0 to 11), say Alice and Bob have shot ak and bk arrows on that section respectively. If ak >= bk, then Alice takes k points. If ak < bk, then Bob takes k points.ak == bk == 0, then nobody takes k points.For example, if Alice and Bob both shot 2 arrows on the section with score 11, then Alice takes 11 points. On the other hand, if Alice shot 0 arrows on the section with score 11 and Bob shot 2 arrows on that same section, then Bob takes 11 points.
You are given the integer numArrows and an integer array aliceArrows of size 12, which represents the number of arrows Alice shot on each scoring section from 0 to 11. Now, Bob wants to maximize the total number of points he can obtain.
Return the array bobArrows which represents the number of arrows Bob shot on each scoring section from 0 to 11. The sum of the values in bobArrows should equal numArrows.
If there are multiple ways for Bob to earn the maximum total points, return any one of them.
Example 1:
Input: numArrows = 9, aliceArrows = [1,1,0,1,0,0,2,1,0,1,2,0] Output: [0,0,0,0,1,1,0,0,1,2,3,1] Explanation: The table above shows how the competition is scored. Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 = 47. It can be shown that Bob cannot obtain a score higher than 47 points.
Example 2:
Input: numArrows = 3, aliceArrows = [0,0,1,0,0,0,0,0,0,0,0,2] Output: [0,0,0,0,0,0,0,0,1,1,1,0] Explanation: The table above shows how the competition is scored. Bob earns a total point of 8 + 9 + 10 = 27. It can be shown that Bob cannot obtain a score higher than 27 points.
Constraints:
1 <= numArrows <= 105aliceArrows.length == bobArrows.length == 120 <= aliceArrows[i], bobArrows[i] <= numArrowssum(aliceArrows[i]) == numArrowsProblem summary: Alice and Bob are opponents in an archery competition. The competition has set the following rules: Alice first shoots numArrows arrows and then Bob shoots numArrows arrows. The points are then calculated as follows: The target has integer scoring sections ranging from 0 to 11 inclusive. For each section of the target with score k (in between 0 to 11), say Alice and Bob have shot ak and bk arrows on that section respectively. If ak >= bk, then Alice takes k points. If ak < bk, then Bob takes k points. However, if ak == bk == 0, then nobody takes k points. For example, if Alice and Bob both shot 2 arrows on the section with score 11, then Alice takes 11 points. On the other hand, if Alice shot 0 arrows on the section with score 11 and Bob shot 2 arrows on that same section, then Bob takes 11 points. You are given the integer numArrows and an integer array aliceArrows of size 12, which
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Backtracking · Bit Manipulation
9 [1,1,0,1,0,0,2,1,0,1,2,0]
3 [0,0,1,0,0,0,0,0,0,0,0,2]
maximum-product-of-the-length-of-two-palindromic-subsequences)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2212: Maximum Points in an Archery Competition
class Solution {
public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
int st = 0, mx = 0;
int m = aliceArrows.length;
for (int mask = 1; mask < 1 << m; ++mask) {
int cnt = 0, s = 0;
for (int i = 0; i < m; ++i) {
if ((mask >> i & 1) == 1) {
s += i;
cnt += aliceArrows[i] + 1;
}
}
if (cnt <= numArrows && s > mx) {
mx = s;
st = mask;
}
}
int[] ans = new int[m];
for (int i = 0; i < m; ++i) {
if ((st >> i & 1) == 1) {
ans[i] = aliceArrows[i] + 1;
numArrows -= ans[i];
}
}
ans[0] += numArrows;
return ans;
}
}
// Accepted solution for LeetCode #2212: Maximum Points in an Archery Competition
func maximumBobPoints(numArrows int, aliceArrows []int) []int {
st, mx := 0, 0
m := len(aliceArrows)
for mask := 1; mask < 1<<m; mask++ {
cnt, s := 0, 0
for i, x := range aliceArrows {
if mask>>i&1 == 1 {
s += i
cnt += x + 1
}
}
if cnt <= numArrows && s > mx {
mx = s
st = mask
}
}
ans := make([]int, m)
for i, x := range aliceArrows {
if (st>>i)&1 == 1 {
ans[i] = x + 1
numArrows -= ans[i]
}
}
ans[0] += numArrows
return ans
}
# Accepted solution for LeetCode #2212: Maximum Points in an Archery Competition
class Solution:
def maximumBobPoints(self, numArrows: int, aliceArrows: List[int]) -> List[int]:
st = mx = 0
m = len(aliceArrows)
for mask in range(1, 1 << m):
cnt = s = 0
for i, x in enumerate(aliceArrows):
if mask >> i & 1:
s += i
cnt += x + 1
if cnt <= numArrows and s > mx:
mx = s
st = mask
ans = [0] * m
for i, x in enumerate(aliceArrows):
if st >> i & 1:
ans[i] = x + 1
numArrows -= ans[i]
ans[0] += numArrows
return ans
// Accepted solution for LeetCode #2212: Maximum Points in an Archery Competition
impl Solution {
pub fn maximum_bob_points(num_arrows: i32, alice_arrows: Vec<i32>) -> Vec<i32> {
let mut st = 0;
let mut mx = 0;
let m = alice_arrows.len();
for mask in 1..(1 << m) {
let mut cnt = 0;
let mut s = 0;
for i in 0..m {
if (mask >> i) & 1 == 1 {
s += i as i32;
cnt += alice_arrows[i] + 1;
}
}
if cnt <= num_arrows && s > mx {
mx = s;
st = mask;
}
}
let mut ans = vec![0; m];
let mut num_arrows = num_arrows;
for i in 0..m {
if (st >> i) & 1 == 1 {
ans[i] = alice_arrows[i] + 1;
num_arrows -= ans[i];
}
}
ans[0] += num_arrows;
ans
}
}
// Accepted solution for LeetCode #2212: Maximum Points in an Archery Competition
function maximumBobPoints(numArrows: number, aliceArrows: number[]): number[] {
let [st, mx] = [0, 0];
const m = aliceArrows.length;
for (let mask = 1; mask < 1 << m; mask++) {
let [cnt, s] = [0, 0];
for (let i = 0; i < m; i++) {
if ((mask >> i) & 1) {
cnt += aliceArrows[i] + 1;
s += i;
}
}
if (cnt <= numArrows && s > mx) {
mx = s;
st = mask;
}
}
const ans: number[] = Array(m).fill(0);
for (let i = 0; i < m; i++) {
if ((st >> i) & 1) {
ans[i] = aliceArrows[i] + 1;
numArrows -= ans[i];
}
}
ans[0] += numArrows;
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
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: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.