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++
#include <bits/stdc++.h>
using namespace std;
int CARDS = 100;
void currentRound(vector< int >& list, int totalPiles)
{
int i;
vector<vector< int > > piles;
for (i = 0; i < totalPiles; i++) {
vector< int > v1;
piles.push_back(v1);
}
int j = 0;
for ( int i = 0; i < CARDS; i++) {
piles[j].push_back(list[i]);
j = (j + 1) % totalPiles;
}
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++;
}
}
int performRounds( int piles[], int rounds, int n)
{
vector< int > list;
for ( int i = 1; i <= CARDS; i++)
list.push_back(i);
for ( int i = 0; i < rounds; i++)
currentRound(list, piles[i]);
return list[n];
}
int main()
{
int piles[] = { 2, 2 };
int rounds = sizeof (piles) / sizeof (piles[0]);
int n = 4;
n--;
cout << performRounds(piles, rounds, n) << endl;
}
|
Java
import java.util.*;
class GFG {
static final int CARDS = 100 ;
static void currentRound(List<Integer> list, int totalPiles)
{
List<List<Integer> > piles = new ArrayList<>();
for ( int i = 0 ; i < totalPiles; i++)
piles.add( new ArrayList<Integer>());
int j = 0 ;
for ( int i = 0 ; i < CARDS; i++) {
piles.get(j).add(list.get(i));
j = (j + 1 ) % totalPiles;
}
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++;
}
}
static int performRounds( int piles[], int rounds, int n)
{
List<Integer> list = new ArrayList<>();
for ( int i = 1 ; i <= CARDS; i++)
list.add(i);
for ( int i = 0 ; i < rounds; i++)
currentRound(list, piles[i]);
return list.get(n);
}
public static void main(String[] args)
{
int piles[] = { 2 , 2 };
int rounds = piles.length;
int n = 4 ;
n--;
System.out.print(performRounds(piles, rounds, n));
}
}
|
Python3
CARDS = 100 ;
def currentRound(list1, totalPiles):
piles = [];
for i in range (totalPiles):
piles.append([])
j = 0 ;
for i in range (CARDS):
piles[j].append(list1[i]);
j = (j + 1 ) % totalPiles;
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
def performRounds(piles, rounds, n):
list1 = [];
for i in range ( 1 , CARDS + 1 ):
list1.append(i);
for i in range (rounds):
currentRound(list1, piles[i]);
return list1[n];
piles = [ 2 , 2 ];
rounds = len (piles);
n = 4 ;
n - = 1 ;
print (performRounds(piles, rounds, n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int CARDS = 100;
static void currentRound(List< int > list,
int totalPiles)
{
int i;
List<List< int > > piles = new List<List< int >>();
for (i = 0; i < totalPiles; i++)
piles.Add( new List< int >());
int j = 0;
for (i = 0; i < CARDS; i++)
{
piles[j].Add(list[i]);
j = (j + 1) % totalPiles;
}
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++;
}
}
static int performRounds( int []piles,
int rounds, int n)
{
List< int > list = new List< int >();
for ( int i = 1; i <= CARDS; i++)
list.Add(i);
for ( int i = 0; i < rounds; i++)
currentRound(list, piles[i]);
return list[n];
}
public static void Main(String[] args)
{
int []piles = { 2, 2 };
int rounds = piles.Length;
int n = 4;
n--;
Console.WriteLine(performRounds(piles, rounds, n));
}
}
|
Javascript
let CARDS = 100;
function currentRound(list, totalPiles)
{
let i;
let piles = [];
for (i = 0; i < totalPiles; i++)
piles.push([])
let j
= 0;
for (i = 0; i < CARDS; i++) {
piles[j].push(list[i]);
j = (j + 1) % totalPiles;
}
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 performRounds(piles, rounds, n)
{
let list = [];
for ( var i = 1; i <= CARDS; i++)
list.push(i);
for ( var i = 0; i < rounds; i++)
currentRound(list, piles[i]);
return list[n];
}
let piles = [ 2, 2 ];
let rounds = piles.length;
let n = 4;
n--;
console.log(performRounds(piles, rounds, n));
|