Simulate Musical Chairs upto ‘K’ rounds
Given N chairs are arranged in a circle, and M people are standing in the circle facing the chairs. When the music starts playing, the people start walking around the circle of chairs in a clockwise direction. After each round of music, one person from each pair of adjacent people sits in a chair. The music stops after K rounds, and the remaining people take unoccupied chairs, the task is to Determine the final positions of each person after the K rounds of the music.
Note: If a person with the number i is in the jth chair, the output should be the pair (i, j).
Examples:
Input: N = 5, M = 10, K = 7
Output: result = {{2, 7}, {3, 8}, {4, 9}, {0, 5}, {1, 6}}
Explanation: After 7 rounds of the music, the final positions of each person will be as follows:
person 0: position (0 + 7) % 5 = 2
person 1: position (1 + 7) % 5 = 3
person 2: position (2 + 7) % 5 = 4
person 3: position (3 + 7) % 5 = 0
person 4: position (4 + 7) % 5 = 1
person 5: position (5 + 7) % 5 = 2
person 6: position (6 + 7) % 5 = 3
person 7: position (7 + 7) % 5 = 4
person 8: position (8 + 7) % 5 = 0
person 9: position (9 + 7) % 5 = 1
Therefore, the final positions of the people sitting in each chair will be:
chair 0: [2, 7]
chair 1: [3, 8]
chair 2: [4, 9]
chair 3: [0, 5]
chair 4: [1, 6]
Input: N = 6, M = 15, K = 4
Output: result = {{0, 6, 12}, {1, 7, 13}, {2, 8, 14}, {3, 9}, {4, 10}, {5, 11}}
Explanation: After 4 rounds of the music, the final positions of each person will be as follows:
person 0: position (0 + 4) % 6 = 4
person 1: position (1 + 4) % 6 = 5
person 2: position (2 + 4) % 6 = 0
person 3: position (3 + 4) % 6 = 1
person 4: position (4 + 4) % 6 = 2
person 5: position (5 + 4) % 6 = 3
person 6: position (6 + 4) % 6 = 4
person 7: position (7 + 4) % 6 = 5
person 8: position (8 + 4) % 6 = 0
person 9: position (9 + 4) % 6 = 1
person 10: position (10 + 4) % 6 = 2
person 11: position (11 + 4) % 6 = 3
person 12: position (12 + 4) % 6 = 4
person 13: position (13 + 4) % 6 = 5
person 14: position (14 + 4) % 6 = 0
Therefore, the final positions of the people sitting in each chair will be:
chair 0: [2, 8, 14]
chair 1: [3, 9]
chair 2: [4, 10]
chair 3: [5, 11]
chair 4: [0, 6, 12]
chair 5: [1, 7, 13]
Naive approach: The basic way to solve the problem is as follows:
The brute force approach to solving the “Musical Chairs” problem is to generate all possible final positions of each person after K rounds of music, and then check if any two persons end up at the same position. This approach involves checking every combination of two persons and verifying if their final positions match.
Here’s how the brute force approach can be implemented:
- Initialize two loops, one outer loop from 0 to M – 1 and the inner loop from outer_loop + 1 to M.
- In each iteration of the inner loop, calculate the final positions of two persons, p1 and p2, as (outer_loop + K) % N and (inner_loop + K) % N, respectively.
- Check if p1 and p2 are equal. If they are, store the indices of both persons in a list.
- Repeat steps 2 and 3 for all combinations of two persons.
- Repeat steps 1 to 4 until the inner loop completes its execution.
- Return the list of indices of persons who ended up in the same position.
Time complexity: O(M^2) as it requires checking every combination of two persons.
Auxiliary space: O(M) as we need to store the indices of all pairs of persons who end up at the same position.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector< int > > musicalChairs( int N, int M, int K)
{
vector< int > positions(M);
for ( int i = 0; i < M; ++i) {
positions[i] = i;
}
for ( int i = 0; i < K; ++i) {
for ( int j = 0; j < M; ++j) {
positions[j] = (positions[j] + 1) % N;
}
}
vector<vector< int > > result(N);
for ( int i = 0; i < M; ++i) {
result[positions[i]].push_back(i);
}
return result;
}
int main()
{
vector<vector< int > > ans = musicalChairs(5, 10, 7);
for ( auto vect : ans) {
for ( auto i : vect) {
cout << i << " " ;
}
cout << '\n' ;
}
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<List<Integer> >
musicalChairs( int N, int M, int K)
{
List<Integer> positions = new ArrayList<>();
for ( int i = 0 ; i < M; ++i) {
positions.add(i);
}
for ( int i = 0 ; i < K; ++i) {
for ( int j = 0 ; j < M; ++j) {
positions.set(j,
(positions.get(j) + 1 ) % N);
}
}
List<List<Integer> > result = new ArrayList<>();
for ( int i = 0 ; i < N; ++i) {
result.add( new ArrayList<>());
}
for ( int i = 0 ; i < M; ++i) {
result.get(positions.get(i)).add(i);
}
return result;
}
public static void main(String[] args)
{
List<List<Integer> > ans = musicalChairs( 5 , 10 , 7 );
for (List<Integer> vect : ans) {
for ( int i : vect) {
System.out.print(i + " " );
}
System.out.println();
}
}
}
|
Python3
def musical_chairs(N, M, K):
positions = [i for i in range (M)]
for i in range (K):
for j in range (M):
positions[j] = (positions[j] + 1 ) % N
result = [[] for i in range (N)]
for i in range (M):
result[positions[i]].append(i)
return result
ans = musical_chairs( 5 , 10 , 7 )
for vect in ans:
for i in vect:
print (i, end = ' ' )
print ()
|
Javascript
function musical_chairs(N, M, K) {
let positions = Array.from({length: M}, (_, i) => i);
for (let i = 0; i < K; i++) {
for (let j = 0; j < M; j++) {
positions[j] = (positions[j] + 1) % N;
}
}
let result = Array.from({length: N}, () => []);
for (let i = 0; i < M; i++) {
result[positions[i]].push(i);
}
return result;
}
let result = musical_chairs(5, 10, 7);
console.log(result);
|
C#
using System;
using System.Collections.Generic;
public class Program {
public static List<List< int > >
MusicalChairs( int N, int M, int K)
{
int [] positions = new int [M];
for ( int i = 0; i < M; ++i) {
positions[i] = i;
}
for ( int i = 0; i < K; ++i) {
for ( int j = 0; j < M; ++j) {
positions[j] = (positions[j] + 1) % N;
}
}
List<List< int > > result = new List<List< int > >(N);
for ( int i = 0; i < N; ++i) {
result.Add( new List< int >());
}
for ( int i = 0; i < M; ++i) {
result[positions[i]].Add(i);
}
return result;
}
public static void Main()
{
List<List< int > > ans = MusicalChairs(5, 10, 7);
foreach ( var vect in ans)
{
foreach ( var i in vect)
{
Console.Write(i + " " );
}
Console.WriteLine();
}
}
}
|
Output
3 8
4 9
0 5
1 6
2 7
Efficient Approach: The algorithm works as follows:
- We calculate the number of iterations ‘num_iterations’ as the remainder of K divided by N. This is because after every N iteration, the people return to their original positions, so we only need to calculate the position of the people after the remaining iterations.
- We create a 1D vector ‘positions’ of size M to store the positions of each person.
- We fill in the position of each person in the ‘positions’ vector by iterating over the M people and calculating their position as the sum of their initial position and the number of iterations (‘num_iterations’), modulo N. This ensures that we always get a valid position within the range of 0 to N-1.
- We create a 2D vector ‘result’ of size N to store the positions of the people. This vector will store the indices of the people that sit in each chair.
- We fill in the ‘result’ vector by iterating over the M people and pushing their index to the corresponding chair in the ‘result’ vector based on their position in the ‘positions’ vector.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<vector< int > > musicalChairs( int N, int M, int K)
{
int numIterations = K % N;
vector< int > positions(M);
for ( int i = 0; i < M; ++i) {
positions[i] = (i + numIterations) % N;
}
vector<vector< int > > result(N);
for ( int i = 0; i < M; ++i) {
result[positions[i]].push_back(i);
}
return result;
}
int main()
{
vector<vector< int > > ans = musicalChairs(5, 10, 7);
for ( auto vect : ans) {
for ( auto i : vect) {
cout << i << " " ;
}
cout << '\n' ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static List<List<Integer> >
musicalChairs( int N, int M, int K)
{
int numIterations = K % N;
List<Integer> positions = new ArrayList<>();
for ( int i = 0 ; i < M; ++i) {
positions.add(i);
}
for ( int i = 0 ; i < M; ++i) {
positions.set(
i, (positions.get(i) + numIterations) % N);
}
List<List<Integer> > result = new ArrayList<>();
for ( int i = 0 ; i < N; ++i) {
result.add( new ArrayList<>());
}
for ( int i = 0 ; i < M; ++i) {
result.get(positions.get(i)).add(i);
}
return result;
}
public static void main(String[] args)
{
List<List<Integer> > ans = musicalChairs( 5 , 10 , 7 );
for (List<Integer> list : ans) {
for ( int i : list) {
System.out.print(i + " " );
}
System.out.println();
}
}
}
|
Python3
def musical_chairs(N, M, K):
num_iterations = K % N
positions = [ 0 ] * M
for i in range (M):
positions[i] = (i + num_iterations) % N
result = [[] for _ in range (N)]
for i in range (M):
result[positions[i]].append(i)
return result
ans = musical_chairs( 5 , 10 , 7 )
for vect in ans:
for i in vect:
print (i, end = " " )
print ()
|
Javascript
function musical_chairs(N, M, K) {
let numIterations = K % N;
let positions = new Array(M);
for (let i = 0; i < M; ++i) {
positions[i] = (i + numIterations) % N;
}
let result = new Array(N);
for (let i = 0; i < N; ++i) {
result[i] = [];
}
for (let i = 0; i < M; ++i) {
result[positions[i]].push(i);
}
return result;
}
let ans = musical_chairs(5, 10, 7);
for (let i = 0; i < ans.length; ++i) {
console.log(ans[i]);
}
|
C#
using System;
using System.Collections.Generic;
class Program {
static List<List< int > > MusicalChairs( int N, int M,
int K)
{
int num_iterations = K % N;
List< int > positions = new List< int >(M);
for ( int i = 0; i < M; ++i) {
positions.Add((i + num_iterations) % N);
}
List<List< int > > result = new List<List< int > >(N);
for ( int i = 0; i < N; ++i) {
result.Add( new List< int >());
}
for ( int i = 0; i < M; ++i) {
result[positions[i]].Add(i);
}
return result;
}
static void Main( string [] args)
{
List<List< int > > ans = MusicalChairs(5, 10, 7);
foreach (List< int > vect in ans)
{
foreach ( int i in vect)
{
Console.Write(i + " " );
}
Console.WriteLine();
}
Console.ReadLine();
}
}
|
Output
3 8
4 9
0 5
1 6
2 7
Time Complexity: O(M) is the time required to fill in the ‘positions’ and ‘result’ vectors.
Auxiliary Space: O(N+M) is the space required to store the ‘positions’ and ‘result’ vectors.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...