Skip to content
Related Articles

Related Articles

Card Shuffle Problem | TCS Digital Advanced Coding Question
  • Difficulty Level : Medium
  • Last Updated : 18 Oct, 2019

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:

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));
    }
}

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
Output:
13

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :