Deal Cards Equally - Problem

You are given an array cards representing a deck of cards (as integers) and an integer k representing the number of players.

Deal the cards one by one in round-robin order to each player: the first card goes to Player 1, the second to Player 2, …, the k-th card to Player k, the (k+1)-th card back to Player 1, and so on.

Return a 2D array where the i-th sub-array contains the cards dealt to Player i+1, sorted in ascending order.

If there are leftover cards that don't fill a complete round, they are still dealt in order.

Input & Output

Example 1 — Even Deal
$ Input: cards = [10,20,30,40,50,60], k = 3
Output: [[10,40],[20,50],[30,60]]
💡 Note: Player 1 gets cards at index 0,3 → [10,40]. Player 2 gets index 1,4 → [20,50]. Player 3 gets index 2,5 → [30,60].
Example 2 — Uneven Deal
$ Input: cards = [5,3,8,1,7], k = 2
Output: [[1,5,8],[3,7]]
💡 Note: Player 1 gets index 0,2,4 → [5,8,7] → sorted [5,7,8]. Player 2 gets index 1,3 → [3,1] → sorted [1,3].
Example 3 — Single Player
$ Input: cards = [9,2,6], k = 1
Output: [[2,6,9]]
💡 Note: Only one player gets all cards → sorted [2,6,9].

Constraints

  • 1 ≤ cards.length ≤ 1000
  • 1 ≤ cards[i] ≤ 10000
  • 1 ≤ k ≤ cards.length

Visualization

Tap to expand
Deal Cards Equally INPUT Card Deck: 10 20 30 40 50 idx 0 idx 1 idx 2 idx 3 idx 4 60 idx 5 k = 3 players Distribution Rule: Card at index j goes to Player = j % k Question: What does each player's sorted hand look like? ALGORITHM STEPS 1 Initialize hands = [ [], [], [] ] for k=3 2 Deal Round-Robin for j in range(6): hands[j%3].append 3 Sort Each Hand Sort each player's cards ascending 4 Return All Hands Return the 2D array of sorted hands Execution Trace: j card j%3 player 0 10 0 P1 1 20 1 P2 2 30 2 P3 3 40 0 P1 4 50 1 P2 5 60 2 P3 All 6 cards dealt evenly! FINAL RESULT Player Hands: 🃏 Player 1 10 40 🃏 Player 2 20 50 🃏 Player 3 30 60 Output: [[10,40],[20,50],[30,60]] 💡 Key Insight: Card at index j always goes to player j % k. This naturally distributes cards round-robin. After dealing, sort each player's hand. Time: O(n log n), Space: O(n). TutorialsPoint - Deal Cards Equally | Modulo Distribution Approach
Asked in
Amazon 30 Google 18 Microsoft 15
8 Views
Medium Frequency
~8 min Avg. Time
0 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