Open In App

Card Shuffle Problem | TCS Digital Advanced Coding Question

Improve
Improve
Like Article
Like
Save
Share
Report

You have 100 cards, numbered 1 to 100. You distribute them into k piles and collect back the piles in order. For example, if you distribute them into 4 piles, then the first pile will contain the cards numbered 1, 5, 9, … and the 4th pile will contain the cards numbered 4, 8, 12, … While collecting back the cards you collect first the last pile, flip it bottom to top, then take the third pile, flip it bottom to top and put the cards on top of the 4th pile and so on. Next round, you distribute the cards into another set of piles and collect in the same manner (last pile first and first pile last). If we have 10 cards and put them into 2 piles, the order of the cards in the piles (top to bottom) would be 9, 7, 5, 3, 1 and 10, 8, 6, 4, 2 We flip the piles to get the order 1, 3, 5, 7, 9 and 2, 4, 6, 8, 10 We put the second pile at the bottom and first on top of it to get the deck 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 Given the number of rounds (m), the number of piles in each round (ki), you need to write a program to find the Nth card from the top at the end of the final round. Input: An array arr[] representing the number of piles in each of the round. Output: One integer representing the Nth card after all rounds have been played. Constraints: Number of rounds ? 10, number of piles in each round ? 13. Examples:

Input: arr[] = {2, 2}, N = 4 Output: 13 We have two rounds. The first round has two piles. At the end of the round, the deck is in the following order: 1, 3, 5, …, 99, 2, 4, 6, …, 100 The next round also has 2 piles and after the second round, the cards are in the order 1, 5, 9, 13, … The fourth card from the top has number 13. Input: arr[] = {2, 2, 3, 8}, N = 18 Output: 100

Approach: For every round create empty ArrayLists for each pile then insert the numbers (card numbers) in these lists as described in the problem then update the original list of card numbers after each round. At the end of the last round, print the nth integer from the original (updated) list.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Total cards
int CARDS = 100;
  
// Function to perform the current round
void currentRound(vector<int>& list, int totalPiles)
{
    int i;
  
    // Create the required empty piles
    vector<vector<int> > piles;
    for (i = 0; i < totalPiles; i++) {
        vector<int> v1;
        piles.push_back(v1);
    }
  
    // Add cards to the piles one by one
    int j = 0;
    for (int i = 0; i < CARDS; i++) {
        piles[j].push_back(list[i]);
        j = (j + 1) % totalPiles;
    }
  
    // After all the piles have been reversed
    // the new order will be first card of the
    // first pile, second card of the first
    // pile, ..., last pile of the last pile
    // (from top to bottom)
    int pileNo = 0;
    i = 0;
    j = 0;
    while (i < CARDS) {
        list.insert(list.begin() + i, piles[pileNo][j]);
        j++;
        if (j >= piles[pileNo].size()) {
            pileNo++;
            j = 0;
        }
        i++;
    }
}
  
// Function to perform all the rounds
int performRounds(int piles[], int rounds, int n)
{
  
    // Create the initial list with all the cards
    vector<int> list;
    for (int i = 1; i <= CARDS; i++)
        list.push_back(i);
  
    // Perform all the rounds
    for (int i = 0; i < rounds; i++)
        currentRound(list, piles[i]);
  
    // Return the nth card
    return list[n];
}
  
// Driver code
int main()
{
    int piles[] = { 2, 2 };
    int rounds = sizeof(piles) / sizeof(piles[0]);
    int n = 4;
  
    // nth card will be at (n - 1)th index
    n--;
    cout << performRounds(piles, rounds, n) << endl;
}
  
// This code is contributed by phasing17


Java




// Java implementation of the approach
import java.util.*;
class GFG {
  
    // Total cards
    static final int CARDS = 100;
  
    // Function to perform the current round
    static void currentRound(List<Integer> list, int totalPiles)
    {
  
        // Create the required empty piles
        List<List<Integer> > piles = new ArrayList<>();
        for (int i = 0; i < totalPiles; i++)
            piles.add(new ArrayList<Integer>());
  
        // Add cards to the piles one by one
        int j = 0;
        for (int i = 0; i < CARDS; i++) {
            piles.get(j).add(list.get(i));
            j = (j + 1) % totalPiles;
        }
  
        // After all the piles have been reversed
        // the new order will be first card of the
        // first pile, second card of the first
        // pile, ..., last pile of the last pile
        // (from top to bottom)
        int pileNo = 0, i = 0;
        j = 0;
        while (i < CARDS) {
            list.set(i, piles.get(pileNo).get(j));
            j++;
            if (j >= piles.get(pileNo).size()) {
                pileNo++;
                j = 0;
            }
            i++;
        }
    }
  
    // Function to perform all the rounds
    static int performRounds(int piles[], int rounds, int n)
    {
  
        // Create the initial list with all the cards
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= CARDS; i++)
            list.add(i);
  
        // Perform all the rounds
        for (int i = 0; i < rounds; i++)
            currentRound(list, piles[i]);
  
        // Return the nth card
        return list.get(n);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int piles[] = { 2, 2 };
        int rounds = piles.length;
        int n = 4;
  
        // nth card will be at (n - 1)th index
        n--;
        System.out.print(performRounds(piles, rounds, n));
    }
}


Python3




# Python3 implementation of the approach
  
# Total cards
CARDS = 100;
  
# Function to perform the current round
def currentRound(list1, totalPiles):
  
    # Create the required empty piles
    piles = [];
    for i in range(totalPiles):
        piles.append([])
  
        # Add cards to the piles one by one
    j = 0;
    for i in range(CARDS):
        piles[j].append(list1[i]);
        j = (j + 1) % totalPiles;
      
    # After all the piles have been reversed
    # the new order will be first card of the
    # first pile, second card of the first
    # pile, ..., last pile of the last pile
    # (from top to bottom)
    pileNo = 0;
    i = 0;
    j = 0;
    while (i < CARDS):
        list1.insert(i, piles[pileNo][j])
        j += 1
        if (j >= len(piles[pileNo])): 
            pileNo += 1
            j = 0;
          
        i += 1
      
# Function to perform all the rounds
def performRounds(piles, rounds, n):
  
    # Create the initial list1 with all the cards
    list1 = [];
    for i in range(1, CARDS + 1):
        list1.append(i);
  
    # Perform all the rounds
    for i in range(rounds):
        currentRound(list1, piles[i]);
  
    # Return the nth card
    return list1[n];
  
# Driver code
piles = [ 2, 2 ];
rounds = len(piles);
n = 4;
  
# nth card will be at (n - 1)th index
n -= 1;
print(performRounds(piles, rounds, n));
  
# This code is contributed by phasing17


C#




// C# implementation of the approach
using System;
using System.Collections.Generic;             
  
class GFG 
{
  
    // Total cards
    static int CARDS = 100;
  
    // Function to perform the current round
    static void currentRound(List<int> list, 
                                  int totalPiles)
    {
        int i;
          
        // Create the required empty piles
        List<List<int> > piles = new List<List<int>>();
        for (i = 0; i < totalPiles; i++)
            piles.Add(new List<int>());
  
        // Add cards to the piles one by one
        int j = 0;
        for (i = 0; i < CARDS; i++) 
        {
            piles[j].Add(list[i]);
            j = (j + 1) % totalPiles;
        }
  
        // After all the piles have been reversed
        // the new order will be first card of the
        // first pile, second card of the first
        // pile, ..., last pile of the last pile
        // (from top to bottom)
        int pileNo = 0; i = 0;
        j = 0;
        while (i < CARDS)
        {
            list.Insert(i, piles[pileNo][j]);
            j++;
            if (j >= piles[pileNo].Count) 
            {
                pileNo++;
                j = 0;
            }
            i++;
        }
    }
  
    // Function to perform all the rounds
    static int performRounds(int []piles, 
                             int rounds, int n)
    {
  
        // Create the initial list with all the cards
        List<int> list = new List<int>();
        for (int i = 1; i <= CARDS; i++)
            list.Add(i);
  
        // Perform all the rounds
        for (int i = 0; i < rounds; i++)
            currentRound(list, piles[i]);
  
        // Return the nth card
        return list[n];
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []piles = { 2, 2 };
        int rounds = piles.Length;
        int n = 4;
  
        // nth card will be at (n - 1)th index
        n--;
        Console.WriteLine(performRounds(piles, rounds, n));
    }
}
  
// This code is contributed by PrinciRaj1992


Javascript




// JS implementation of the approach
  
// Total cards
let CARDS = 100;
  
// Function to perform the current round
function currentRound(list, totalPiles)
{
    let i;
  
    // Create the required empty piles
    let piles = [];
    for (i = 0; i < totalPiles; i++)
        piles.push([])
  
            // Add cards to the piles one by one
            let j
            = 0;
    for (i = 0; i < CARDS; i++) {
        piles[j].push(list[i]);
        j = (j + 1) % totalPiles;
    }
  
    // After all the piles have been reversed
    // the new order will be first card of the
    // first pile, second card of the first
    // pile, ..., last pile of the last pile
    // (from top to bottom)
    let pileNo = 0;
    i = 0;
    j = 0;
    while (i < CARDS) {
        list.splice(i, 0, piles[pileNo][j]);
        j++;
        if (j >= piles[pileNo].length) {
            pileNo++;
            j = 0;
        }
        i++;
    }
}
  
// Function to perform all the rounds
function performRounds(piles, rounds, n)
{
  
    // Create the initial list with all the cards
    let list = [];
    for (var i = 1; i <= CARDS; i++)
        list.push(i);
  
    // Perform all the rounds
    for (var i = 0; i < rounds; i++)
        currentRound(list, piles[i]);
  
    // Return the nth card
    return list[n];
}
  
// Driver code
let piles = [ 2, 2 ];
let rounds = piles.length;
let n = 4;
  
// nth card will be at (n - 1)th index
n--;
console.log(performRounds(piles, rounds, n));
  
// This code is contributed by PrinciRaj1992


Output:

13


Last Updated : 11 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads