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 INPUT nums array (even length) 5 idx 0 4 idx 1 2 idx 2 3 idx 3 Players: A Alice B Bob Result array arr = [ ] nums = [5, 4, 2, 3] Length = 4 (even) ALGORITHM STEPS 1 Sort Array [5,4,2,3] --> [2,3,4,5] 2 Process Pairs Take 2 elements at a time 3 Swap in Pairs Bob first, then Alice 4 Build Result Append swapped pairs to arr Rounds (sorted: [2,3,4,5]) Round 1: Alice:2, Bob:3 arr=[3,2] Round 2: Alice:4, Bob:5 arr=[3,2,5,4] Pattern: swap(arr[i], arr[i+1]) for each pair FINAL RESULT Resulting array arr 3 idx 0 2 idx 1 5 idx 2 4 idx 3 arr = [3, 2, 5, 4] OK - Verified! How it works: 1. Sort: [2,3,4,5] 2. Pair (2,3): Bob adds 3, then Alice adds 2 3. Pair (4,5): Bob adds 5, then Alice adds 4 Key Insight: Sort the array first, then swap adjacent pairs. Since Alice always picks minimum first, followed by Bob, but Bob appends first to arr - we simply sort and swap every pair. Time: O(n log n), Space: O(1) TutorialsPoint - Minimum Number Game | Optimal Solution
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