Minimum Number Game - Problem

Alice and Bob are playing a competitive game with an array of numbers! 🎮

You are given a 0-indexed integer array nums of even length and there is also an empty array arr. Alice and Bob take turns in a specific pattern:

  • Round Rules:
    • First, Alice removes the minimum element from nums
    • Then, Bob removes the minimum element from the remaining nums
    • Next, Bob appends his removed element to arr
    • Finally, Alice appends her removed element to arr

The game continues until nums becomes empty. Your task is to return the resulting array arr after all rounds are complete.

Example: If nums = [5,4,2,3], Alice takes 2 (min), Bob takes 3 (new min), Bob adds 3 first, Alice adds 2, giving [3,2]. Next round: Alice takes 4, Bob takes 5, result: [3,2,5,4].

Input & Output

example_1.py — Python
$ Input: nums = [5,4,2,3]
Output: [3,2,5,4]
💡 Note: Round 1: Alice takes 2 (minimum), Bob takes 3 (next minimum). Bob appends 3, Alice appends 2 → [3,2]. Round 2: Alice takes 4, Bob takes 5. Bob appends 5, Alice appends 4 → [3,2,5,4]
example_2.py — Python
$ Input: nums = [2,5]
Output: [5,2]
💡 Note: Only one round: Alice takes 2, Bob takes 5. Bob appends first (5), then Alice (2) → [5,2]
example_3.py — Python
$ Input: nums = [1,2,3,4,5,6]
Output: [2,1,4,3,6,5]
💡 Note: Round 1: Alice→1, Bob→2, result [2,1]. Round 2: Alice→3, Bob→4, result [2,1,4,3]. Round 3: Alice→5, Bob→6, result [2,1,4,3,6,5]. Pattern: swap each consecutive pair from sorted array.

Constraints

  • 2 ≤ nums.length ≤ 100
  • nums.length is even
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
🎮 Alice & Bob's Minimum Number GameOriginal Array:5423Sort first!Sorted Array:2345👩 Alice picks:2 (minimum)👨 Bob picks:3 (next minimum)Game Result (Bob places first!):3Bob2Alice5Bob4Alice🔑 Key Pattern Recognition• Alice and Bob always pick the 2 smallest available• This means we can sort first, then process pairs• Since Bob appends first, we swap each consecutive pair• Result: [sorted[1], sorted[0], sorted[3], sorted[2], ...]
Understanding the Visualization
1
Sort the treasures
Arrange all treasures by value: [2,3,4,5]
2
First round picks
Alice gets smallest (2), Bob gets next smallest (3)
3
First round placement
Bob places first: [3], then Alice: [3,2]
4
Continue pattern
Repeat for remaining pairs: [3,2,5,4]
Key Takeaway
🎯 Key Insight: Instead of simulating the game step-by-step (O(n²)), we recognize that the result is simply the sorted array with consecutive pairs swapped, achieving O(n log n) time complexity.
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
24.5K Views
Medium Frequency
~15 min Avg. Time
890 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