Card Shuffle Problem | TCS Digital Advanced Coding Question | Java 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
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 ) ) ; } }