Maximum Points in an Archery Competition - Problem

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 calculated as follows:
    • The target has integer scoring sections ranging from 0 to 11 inclusive.
    • For each section of the target with score k (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.

Input & Output

Example 1 — Strategic Competition
$ 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]
💡 Note: Bob competes for sections 4,5,8,9,10,11. He needs 1+1+1+2+3+1=9 arrows total. This gives him 4+5+8+9+10+11=47 points, while Alice gets 1+1+3+7+1=13 points from sections 0,1,3,6,7.
Example 2 — Focus on High Value
$ 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]
💡 Note: Bob has only 3 arrows. He competes for sections 8,9,10 (needing 1 arrow each) to get 8+9+10=27 points. Alice gets 2+11=13 points from sections 2,11.
Example 3 — All Arrows to One Section
$ Input: numArrows = 2, aliceArrows = [1,1,1,1,1,1,1,1,1,1,1,1]
Output: [0,0,0,0,0,0,0,0,0,0,0,2]
💡 Note: Best strategy is to compete only for section 11 (highest value) with 2 arrows, beating Alice's 1 arrow. Bob gets 11 points, Alice gets 0+1+2+3+4+5+6+7+8+9+10=55 points.

Constraints

  • 1 ≤ numArrows ≤ 105
  • aliceArrows.length == 12
  • 0 ≤ aliceArrows[i] ≤ numArrows
  • sum(aliceArrows[i]) == numArrows

Visualization

Tap to expand
Archery Competition: Bob vs AliceSections:0 1 2 3 4 5 6 7 8 9 10 11Alice:110100210120Bob:000011001231Bob competes for sections 4,5,8,9,10,11Alice Score: 0+1+3+6+7 = 17Bob Score: 4+5+8+9+10+11 = 47Bob Wins!Total arrows used: 9 (1+1+1+2+3+1)
Understanding the Visualization
1
Input
numArrows=9, Alice's distribution across 12 sections
2
Strategy
Bob decides which sections to compete for
3
Output
Bob's optimal arrow distribution
Key Takeaway
🎯 Key Insight: Bob needs exactly aliceArrows[i] + 1 arrows to win section i
Asked in
Amazon 15 Google 12 Meta 8
28.5K Views
Medium Frequency
~35 min Avg. Time
924 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