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.
N = 3
Firstly the 3rd glass contain the ball.
After the first shuffle operation (3, 1), 1st glass contain the ball.
After the second shuffle operation (2, 1), 2nd glass contain the ball.
After the third shuffle operation (1, 2), 1st glass contain the ball.
N = 1
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:
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.
- Find the position of box which occupies the given ball
- Count of elements which are not at the correct position
- Find total no of collisions taking place between the balls in which initial direction of each ball is given
- RGYB(color) Slots Game to guess the correct color for the correct slot
- Maximum sum of values in a given range of an Array for Q queries when shuffling is allowed
- Find the position of the given row in a 2-D array
- Find the value at kth position in the generated array
- Find the Kth position element of the given sequence
- Find the position of the given Prime Number
- Find the position of the last removed element from the array
- Find position of the given number among the numbers made of 4 and 7
- Find element position in given monotonic sequence
- Find position of given term in a series formed with only digits 4 and 7 allowed
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard
- In how many ways the ball will come back to the first boy after N turns
- 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
- Sum of even numbers at even position
- Position of n among the numbers made of 2, 3, 5 & 7
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.