Consider a shuffle game. There are **3** glasses numbered from **1 to 3** and one ball is hidden under any one of the glass. **Then any 2 of the glasses are shuffled**. This operation is made 3 times.

Given an integer **N** ranged **[1, 3]** and 3 **pairs of integers** of the same range. The **N-th** glass contain the ball initially and every pair of the given integers represents the indices of the glasses needs to be shuffled. Remember the glasses are renumbered after each shuffle.

The task is to find out the index of the glass which contains the ball after all the shuffle operation.

**Examples:**

Input:

N = 3

3 1

2 1

1 2

Output:1

Firstly the3rdglass contain the ball.

After the first shuffle operation(3, 1),1stglass contain the ball.

After the second shuffle operation(2, 1),2ndglass contain the ball.

After the third shuffle operation(1, 2),1stglass contain the ball.

Input:

N = 1

1 3

1 2

2 3

Output:2

**Approach: **The simplest approach will be to run a loop for every shuffle operation.

If any of the 2 glasses being shuffled contain the ball then it is obvious to change the value of **N** to the index of the glass being shuffled with.

If any of the 2 shuffling glasses doesn’t contain the ball, then nothing needs to be done.

Below is the implementation of the above code:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `M = 3, N = 2; ` ` ` `// Function to generate the index of the glass ` `// containing the ball ` `void` `getIndex(` `int` `n, ` `int` `shuffle[][N]) ` `{ ` ` ` `for` `(` `int` `i = 0; i < 3; i++) { ` ` ` ` ` `// Checking if the glasses ` ` ` `// being shuffled contain ` ` ` `// the ball ` ` ` ` ` `// Change the index ` ` ` `if` `(shuffle[i][0] == n) ` ` ` `n = shuffle[i][1]; ` ` ` ` ` `// Change the index ` ` ` `else` `if` `(shuffle[i][1] == n) ` ` ` `n = shuffle[i][0]; ` ` ` `} ` ` ` ` ` `// Print the index ` ` ` `cout << n; ` `} ` ` ` `// Driver's Code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `// Storing all the shuffle operation ` ` ` `int` `shuffle[M][N] = { ` ` ` `{ 3, 1 }, ` ` ` `{ 2, 1 }, ` ` ` `{ 1, 2 } ` ` ` `}; ` ` ` ` ` `getIndex(n, shuffle); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `M = ` `3` `; ` `static` `int` `N = ` `2` `; ` ` ` `// Function to generate the index of the glass ` `// containing the ball ` `static` `void` `getIndex(` `int` `n, ` `int` `shuffle[][]) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `3` `; i++) ` ` ` `{ ` ` ` ` ` `// Checking if the glasses ` ` ` `// being shuffled contain ` ` ` `// the ball ` ` ` ` ` `// Change the index ` ` ` `if` `(shuffle[i][` `0` `] == n) ` ` ` `n = shuffle[i][` `1` `]; ` ` ` ` ` `// Change the index ` ` ` `else` `if` `(shuffle[i][` `1` `] == n) ` ` ` `n = shuffle[i][` `0` `]; ` ` ` `} ` ` ` ` ` `// Print the index ` ` ` `System.out.println (n); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `// Storing all the shuffle operation ` ` ` `int` `shuffle[][] = {{ ` `3` `, ` `1` `}, ` ` ` `{ ` `2` `, ` `1` `}, ` ` ` `{ ` `1` `, ` `2` `}}; ` ` ` ` ` `getIndex(n, shuffle); ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# the above approach ` `M ` `=` `3` `; N ` `=` `2` `; ` ` ` `# Function to generate the index of ` `# the glass containing the ball ` `def` `getIndex(n, shuffle) : ` ` ` ` ` `for` `i ` `in` `range` `(` `3` `) : ` ` ` ` ` `# Checking if the glasses ` ` ` `# being shuffled contain ` ` ` `# the ball ` ` ` ` ` `# Change the index ` ` ` `if` `(shuffle[i][` `0` `] ` `=` `=` `n) : ` ` ` `n ` `=` `shuffle[i][` `1` `]; ` ` ` ` ` `# Change the index ` ` ` `elif` `(shuffle[i][` `1` `] ` `=` `=` `n) : ` ` ` `n ` `=` `shuffle[i][` `0` `]; ` ` ` ` ` `# Print the index ` ` ` `print` `(n); ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `3` `; ` ` ` ` ` `# Storing all the shuffle operation ` ` ` `shuffle ` `=` `[[ ` `3` `, ` `1` `], ` ` ` `[ ` `2` `, ` `1` `], ` ` ` `[ ` `1` `, ` `2` `]]; ` ` ` ` ` `getIndex(n, shuffle); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `M = 3; ` `static` `int` `N = 2; ` ` ` `// Function to generate the index of ` `// the glass containing the ball ` `static` `void` `getIndex(` `int` `n, ` `int` `[,]shuffle) ` `{ ` ` ` `for` `(` `int` `i = 0; i < 3; i++) ` ` ` `{ ` ` ` ` ` `// Checking if the glasses ` ` ` `// being shuffled contain ` ` ` `// the ball ` ` ` ` ` `// Change the index ` ` ` `if` `(shuffle[i, 0] == n) ` ` ` `n = shuffle[i, 1]; ` ` ` ` ` `// Change the index ` ` ` `else` `if` `(shuffle[i, 1] == n) ` ` ` `n = shuffle[i, 0]; ` ` ` `} ` ` ` ` ` `// Print the index ` ` ` `Console.WriteLine(n); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main (String[] args) ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `// Storing all the shuffle operation ` ` ` `int` `[,]shuffle = {{ 3, 1 }, ` ` ` `{ 2, 1 }, ` ` ` `{ 1, 2 }}; ` ` ` ` ` `getIndex(n, shuffle); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

1

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:

- RGYB(color) Slots Game to guess the correct color for the correct slot
- Find the position of box which occupies the given ball
- In how many ways the ball will come back to the first boy after N turns
- Count of elements which are not at the correct position
- Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed
- Count of distinct numbers formed by shuffling the digits of a large number N
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- Puzzle | Weight of Heavy Ball
- Ways to choose balls such that at least one ball is chosen
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Position of an element after stable sort
- Check if it is possible to return to the starting position after moving in the given directions
- Position after taking N steps to the right and left in an alternate manner
- Insert N elements in a Linked List one after other at middle position
- Find out the minimum number of coins required to pay total amount
- Find out the prime numbers in the form of A+nB or B+nA
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Possibility of moving out of maze
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Check if there exists a number with X factors out of which exactly K are prime

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.