Find out the correct position of the ball after shuffling
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 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.
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++
#include <bits/stdc++.h>
using namespace std;
const int M = 3, N = 2;
void getIndex( int n, int shuffle[][N])
{
for ( int i = 0; i < 3; i++) {
if (shuffle[i][0] == n)
n = shuffle[i][1];
else if (shuffle[i][1] == n)
n = shuffle[i][0];
}
cout << n;
}
int main()
{
int n = 3;
int shuffle[M][N] = {
{ 3, 1 },
{ 2, 1 },
{ 1, 2 }
};
getIndex(n, shuffle);
}
|
Java
import java.io.*;
class GFG
{
static int M = 3 ;
static int N = 2 ;
static void getIndex( int n, int shuffle[][])
{
for ( int i = 0 ; i < 3 ; i++)
{
if (shuffle[i][ 0 ] == n)
n = shuffle[i][ 1 ];
else if (shuffle[i][ 1 ] == n)
n = shuffle[i][ 0 ];
}
System.out.println (n);
}
public static void main (String[] args)
{
int n = 3 ;
int shuffle[][] = {{ 3 , 1 },
{ 2 , 1 },
{ 1 , 2 }};
getIndex(n, shuffle);
}
}
|
Python3
M = 3 ; N = 2 ;
def getIndex(n, shuffle) :
for i in range ( 3 ) :
if (shuffle[i][ 0 ] = = n) :
n = shuffle[i][ 1 ];
elif (shuffle[i][ 1 ] = = n) :
n = shuffle[i][ 0 ];
print (n);
if __name__ = = "__main__" :
n = 3 ;
shuffle = [[ 3 , 1 ],
[ 2 , 1 ],
[ 1 , 2 ]];
getIndex(n, shuffle);
|
C#
using System;
class GFG
{
static int M = 3;
static int N = 2;
static void getIndex( int n, int [,]shuffle)
{
for ( int i = 0; i < 3; i++)
{
if (shuffle[i, 0] == n)
n = shuffle[i, 1];
else if (shuffle[i, 1] == n)
n = shuffle[i, 0];
}
Console.WriteLine(n);
}
public static void Main (String[] args)
{
int n = 3;
int [,]shuffle = {{ 3, 1 },
{ 2, 1 },
{ 1, 2 }};
getIndex(n, shuffle);
}
}
|
Javascript
<script>
M = 3;
N = 2;
function getIndex(n , shuffle) {
for (i = 0; i < 3; i++) {
if (shuffle[i][0] == n)
n = shuffle[i][1];
else if (shuffle[i][1] == n)
n = shuffle[i][0];
}
document.write(n);
}
var n = 3;
var shuffle = [ [ 3, 1 ],
[ 2, 1 ],
[ 1, 2 ] ];
getIndex(n, shuffle);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
13 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...