Card Shuffle Problem | TCS Digital Advanced Coding Question | C # implementation of the approach ; Total cards ; Function to perform the current round ; Create the required empty piles ; Add cards to the piles one by one ; 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 ) ; Function to perform all the rounds ; Create the initial list with all the cards ; Perform all the rounds ; Return the nth card ; Driver code ; nth card will be at ( n - 1 ) th index
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 ) ) ; } }