Card Shuffle Problem | TCS Digital Advanced Coding Question
Last Updated :
11 Jan, 2023
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
Similar Reads
TCS SDE Sheet: Interview Questions and Answers
What is TCS NQT ? TCS NQT is National Qualifier Test conducted by Tata Consultancy Services, it is a prerequisite for all the tests. The validity of the NQT score is of two years. Candidates are eligible to apply on the TCS iON Job Listing Portal with an NQT score. NQT exams are conducted in both on
8 min read
TCS National Qualifier 2 Coding Question.
You are given a string and your task is to print the frequency of each character. Direction to Solve Problem: 1. Take string from STDIN. aaaabbBcddee2. Get all different characters in given string using set(). set ={a, b, B, c, d, e} # unordered set3. Iterate for different characters ( len(set )) be
3 min read
Shuffle a pack of cards and answer the query
Given a pack of 2^N cards (0 ... 2^N - 1), shuffle it in N steps. At step k (0 < k < N) we divide the deck into 2k equal-sized decks. Each one of those decks is reordered by having all the cards that lie on even positions first, followed by all cards that lie on odd positions (the order is pre
5 min read
Block Coding in Digital Electronics
The block coding is a technique through which much reliability is injected into the data that is sent over the network or kept in a device. It does this by adding extra information bits to every block of data; hence, it identifies the errors and corrects the same if it happens to occur on the way. B
8 min read
Adaptive Huffman Coding And Decoding
Prerequisite: Huffman Coding, Huffman Decoding Adaptive Huffman Coding is also known as Dynamic Huffman Coding. The implementation is done using Vitter Algorithm. Encoding Adaptive Huffman coding for a string containing alphabets: Let m be the total number of alphabets. So m = 26. For Vitter Algorit
7 min read
TCS NQT Coding Questions & How Coding Task Evaluated in TCS NQT
To know more about the TCS NQT: TCS NQT â National Qualifier Test1. How to solve the Coding Section in TCS NQT 2022? STEP by STEP guide to solving the coding section in TCS NQT 2022.STEP 1: Understanding the storyYou are given 30 minutes to write a program and clear all the test cases. Of this, spen
9 min read
How I cracked TCS Digital
Hurrah! My TCS Digital interview results are out, and I thankfully made it. Having wanted to work in the field of Artificial Intelligence for a long time now, I donât think there is a better platform than TCS Digital for learning how to do so in the professional world, for freshers like us. In this
13 min read
Decode a given pattern in two ways (Flipkart Interview Question)
A sender sends a binary string to a receiver meanwhile he encrypt the digits. You are given a encrypted form of string. Now, the receiver needs to decode the string, and while decoding there were 2 approaches. Let the encrypted binary string be P[] and actual string be S[]. First, receiver starts wi
8 min read
Practice Questions on Huffman Encoding
Huffman Encoding is an important topic from GATE point of view and different types of questions are asked from this topic. Before understanding this article, you should have basic idea about Huffman encoding. These are the types of questions asked in GATE based on Huffman Encoding. Type 1. Conceptua
4 min read
Binary Decoder in Digital Logic
A binary decoder is a digital circuit that converts a binary code into a set of outputs. The binary code represents the position of the desired output and is used to select the specific output that is active. Binary decoders are the inverse of encoders and are commonly used in digital systems to con
7 min read