Skip to main content
Back to problems
#2212
Medium Algorithms

Maximum points in an archery competition

Array Backtracking Bit Manipulation Enumeration
51.4% acceptance
Feb 25, 2026
514
57
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 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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn maximum_bob_points(num_arrows: i32, alice_arrows: Vec<i32>) -> Vec<i32> {
    let mut best_mask = 0u32;
    let mut best_score = 0i32;
    // Try all subsets of sections 1..=11 (2^11 = 2048)
    for mask in 0u32..2048 {
      let mut arrows_needed = 0i32;
      let mut score = 0i32;
      for k in 1..=11 {
        if mask & (1 << (k - 1)) != 0 {
          arrows_needed += alice_arrows[k] + 1;
          score += k as i32;
        }
      }
      if arrows_needed <= num_arrows && score > best_score {
        best_score = score;
        best_mask = mask;
      }
    }
    let mut bob = vec![0i32; 12];
    let mut remaining = num_arrows;
    for k in 1..=11 {
      if best_mask & (1 << (k - 1)) != 0 {
        bob[k] = alice_arrows[k] + 1;
        remaining -= bob[k];
      }
    }
    bob[0] += remaining;
    bob
  }
}