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 preserved in each one of the two subsequences). Now, we are given a key (index). We have to answer the card on that position (0-based indexing). Examples:

Input : N = 3 (Size = 2^N), Key = 3 Output : 6 Explanation : Pack : 0 1 2 3 4 5 6 7 Shuffle 1 : 0 2 4 6|1 3 5 7 Shuffle 2 : 0 4|2 6|1 5|3 7 Card at index 3 : 6

**Method 1 :** We can simply simulate the whole process and find the exact order of the cards after all the N shuffles are done.

Time Complexity : O(N * 2^N)

**Method 2 :**

Let us try to find the binary representation of Key and the final answer and try to spot some observations based on it.

Let N = 3

Below is the table :

Key ANS

000 000

001 100

010 010

011 110

100 001

101 101

110 011

111 111

It is clearly visible that the answer is the reverse of binary representation of Key.

## C++

`// C++ program to find the card at given index ` `// after N shuffles ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find card at given index ` `void` `shuffle(` `int` `N, ` `int` `key) ` `{ ` ` ` ` ` `// Answer will be reversal of N bits from MSB ` ` ` `unsigned ` `int` `NO_OF_BITS = N; ` ` ` `unsigned ` `int` `reverse_num = 0, temp; ` ` ` ` ` `// Calculating the reverse binary representation ` ` ` `for` `(` `int` `i = 0; i < NO_OF_BITS; i++) { ` ` ` `temp = (key & (1 << i)); ` ` ` `if` `(temp) ` ` ` `reverse_num |= (1 << ((NO_OF_BITS - 1) - i)); ` ` ` `} ` ` ` ` ` `// Printing the result ` ` ` `cout << reverse_num; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `// No. of Shuffle Steps ` ` ` `int` `N = 3; ` ` ` ` ` `// Key position ` ` ` `unsigned ` `int` `key = 3; ` ` ` ` ` `shuffle(N, key); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the card at given index ` `// after N shuffles ` `class` `GFG { ` ` ` ` ` `// function to find card at given index ` ` ` `static` `void` `shuffle(` `int` `N, ` `int` `key) ` ` ` `{ ` ` ` ` ` `// Answer will be reversal of N bits from MSB ` ` ` `int` `NO_OF_BITS = N; ` ` ` `int` `reverse_num = ` `0` `, temp; ` ` ` ` ` `// Calculating the reverse binary representation ` ` ` `for` `(` `int` `i = ` `0` `; i < NO_OF_BITS; i++) { ` ` ` `temp = (key & (` `1` `<< i)); ` ` ` `if` `(temp>` `0` `) ` ` ` `reverse_num |= (` `1` `<< ((NO_OF_BITS - ` `1` `) - i)); ` ` ` `} ` ` ` ` ` `// Printing the result ` ` ` `System.out.print(reverse_num); ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `// No. of Shuffle Steps ` ` ` `int` `N = ` `3` `; ` ` ` ` ` `// Key position ` ` ` `int` `key = ` `3` `; ` ` ` ` ` `shuffle(N, key); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the card ` `# at given index after N shuffles ` ` ` `# Function to find card at given index ` `def` `shuffle(N, key): ` ` ` ` ` `# Answer will be reversal ` ` ` `# of N bits from MSB ` ` ` `NO_OF_BITS ` `=` `N ` ` ` `reverse_num ` `=` `0` ` ` ` ` `# Calculating the reverse binary representation ` ` ` `for` `i ` `in` `range` `(NO_OF_BITS): ` ` ` `temp ` `=` `(key & (` `1` `<< i)) ` ` ` `if` `(temp): ` ` ` `reverse_num |` `=` `(` `1` `<< ((NO_OF_BITS ` `-` `1` `) ` `-` `i)) ` ` ` ` ` `# Printing the result ` ` ` `print` `(reverse_num) ` ` ` `# Driver code ` ` ` `# No. of Shuffle Steps ` `N ` `=` `3` ` ` `# Key position ` `key ` `=` `3` `shuffle(N, key) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the card at given index ` `// after N shuffles ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to find card at given index ` ` ` `static` `void` `shuffle(` `int` `N, ` `int` `key) ` ` ` `{ ` ` ` ` ` `// Answer will be reversal of N bits from MSB ` ` ` `int` `NO_OF_BITS = N; ` ` ` `int` `reverse_num = 0, temp; ` ` ` ` ` `// Calculating the reverse binary representation ` ` ` `for` `(` `int` `i = 0; i < NO_OF_BITS; i++) { ` ` ` `temp = (key & (1 << i)); ` ` ` `if` `(temp > 0) ` ` ` `reverse_num |= (1 << ((NO_OF_BITS - 1) - i)); ` ` ` `} ` ` ` ` ` `// Printing the result ` ` ` `Console.Write(reverse_num); ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `// No. of Shuffle Steps ` ` ` `int` `N = 3; ` ` ` ` ` `// Key position ` ` ` `int` `key = 3; ` ` ` ` ` `shuffle(N, key); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

Output:

6

This article is contributed by **Rohit**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Best way to answer If an Interviewer asks about Salary Expectation to you as a Fresher
- Query to count odd and even parity elements in subarray after XOR with K
- Range query for count of set bits
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Generate 0 and 1 with 25% and 75% probability
- What are the differences between bitwise and logical AND operators in C/C++?
- Univariate, Bivariate and Multivariate data and its analysis
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Number of subsets with same AND, OR and XOR values in an Array
- Count subsequences with same values of Bitwise AND, OR and XOR
- Minimum XOR of OR and AND of any pair in the Array
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Construct the Array using given bitwise AND, OR and XOR
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find a number M < N such that difference between their XOR and AND is maximum