Minimum Number Game - Problem

You are given a 0-indexed integer array nums of even length and there is also an empty array arr.

Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:

  • Every round, first Alice will remove the minimum element from nums, and then Bob does the same.
  • Now, first Bob will append the removed element in the array arr, and then Alice does the same.
  • The game continues until nums becomes empty.

Return the resulting array arr.

Input & Output

Example 1 — Basic Case
$ Input: nums = [5,4,2,3]
Output: [3,2,5,4]
💡 Note: Round 1: Alice picks min=2, Bob picks min=3. Add to result: [3,2]. Round 2: Alice picks min=4, Bob picks min=5. Add to result: [3,2,5,4]
Example 2 — Small Array
$ Input: nums = [2,5]
Output: [5,2]
💡 Note: Only one round: Alice picks 2, Bob picks 5. Bob goes first in result: [5,2]
Example 3 — Already Sorted
$ Input: nums = [1,2,3,4]
Output: [2,1,4,3]
💡 Note: Round 1: Alice picks 1, Bob picks 2 → [2,1]. Round 2: Alice picks 3, Bob picks 4 → [2,1,4,3]

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 109
  • nums.length is even

Visualization

Tap to expand
Minimum Number Game: Alice vs BobInput Array:5423Game Rules: Alice picks min first, Bob picks min secondBut Bob adds to result first, then AliceRound 1: Alice picks 2, Bob picks 3 → Add [3,2]Round 2: Alice picks 4, Bob picks 5 → Add [5,4]Final Result:3254
Understanding the Visualization
1
Input
Array [5,4,2,3] and empty result array
2
Game Simulation
Players take turns picking minimums, Bob adds first
3
Output
Final result [3,2,5,4]
Key Takeaway
🎯 Key Insight: Sort first to avoid repeatedly searching for minimums - then process pairs in reverse order
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K 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