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
Maximum Points in an Archery Competition INPUT 11 0-11 pts numArrows=9 Alice shoots first aliceArrows[0..11]: 1 1 0 1 0 0 2 1 0 1 2 0 0 1 2 3 4 5 6 7 8 9 10 11 Alice uses: 1+1+0+1+0+0+2+1+0+1+2+0 = 9 Goal: Find Bob's optimal arrow distribution (9 arrows) ALGORITHM STEPS 1 Greedy Strategy Prioritize high-value sections (more points per arrow cost) 2 Beat Alice's Count Bob needs alice[k]+1 arrows to win section k 3 Calculate Cost/Benefit For each section k: cost = alice[k]+1, gain = k pts Section Analysis (high to low) k=11: cost=1, gain=11 [OK] k=10: cost=3, gain=10 [OK] k=9: cost=2, gain=9 [OK] k=8: cost=1, gain=8 [OK] k=5: cost=1, gain=5 [OK] k=4: cost=1, gain=4 [OK] 4 Allocate 9 Arrows 1+3+2+1+1+1 = 9 arrows used Total: 11+10+9+8+5+4 = 47 pts FINAL RESULT Bob's Winning Strategy 11 Bob wins Alice wins / No contest bobArrows[0..11]: 0 0 0 0 1 1 0 0 1 2 3 1 0 1 2 3 4 5 6 7 8 9 10 11 [0,0,0,0,1,1,0,0,1,2,3,1] Total: 9 arrows Alice Wins: 0,1,3,6,7 17 pts Bob Wins: 4,5,8,9,10,11 47 pts Key Insight: The greedy approach prioritizes winning high-value sections first. To beat Alice at section k, Bob needs alice[k]+1 arrows. By targeting sections 11,10,9,8,5,4 with minimal arrows (1+3+2+1+1+1=9), Bob maximizes points (47) vs Alice (17). TutorialsPoint - Maximum Points in an Archery Competition | Greedy Optimal Solution
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