Open In App

Simulate Musical Chairs upto ‘K’ rounds

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate the final position
// of each player after K rotations
vector<vector<int> > musicalChairs(int N, int M, int K)
{
  
    // An array to store the initial
    // position of each player
    vector<int> positions(M);
    for (int i = 0; i < M; ++i) {
        positions[i] = i;
    }
  
    // Rotate all the players K times
    for (int i = 0; i < K; ++i) {
        for (int j = 0; j < M; ++j) {
  
            // Calculate the next position
            // for each player
            positions[j] = (positions[j] + 1) % N;
        }
    }
  
    // An array of vectors to store the
    // final position of each player
    vector<vector<int> > result(N);
    for (int i = 0; i < M; ++i) {
        result[positions[i]].push_back(i);
    }
  
    // Return the final positions
    return result;
}
  
// Driver's code
int main()
{
  
    // Call the musicalChairs function
    // and store the result in the
    // ans variable
    vector<vector<int> > ans = musicalChairs(5, 10, 7);
  
    // Print the final position
    // of each player
    for (auto vect : ans) {
        for (auto i : vect) {
            cout << i << " ";
        }
        cout << '\n';
    }
    return 0;
}


Java




// Java code to implement the above approach
import java.util.ArrayList;
import java.util.List;
  
public class Main {
    // Function to calculate the final position of each
    // player after K rotations
    public static List<List<Integer> >
    musicalChairs(int N, int M, int K)
    {
        // An array to store the initial position of each
        // player
        List<Integer> positions = new ArrayList<>();
        for (int i = 0; i < M; ++i) {
            positions.add(i);
        }
        // Rotate all the players K times
        for (int i = 0; i < K; ++i) {
            for (int j = 0; j < M; ++j) {
                // Calculate the next position for each
                // player
                positions.set(j,
                              (positions.get(j) + 1) % N);
            }
        }
        // A list of lists to store the final position of
        // each player
        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 the final positions
        return result;
    }
    // Driver's code
    public static void main(String[] args)
    {
        // Call the musicalChairs function and store the
        // result in the ans variable
        List<List<Integer> > ans = musicalChairs(5, 10, 7);
        // Print the final position of each player
        for (List<Integer> vect : ans) {
            for (int i : vect) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}


Python3




# Python code to implement the above approach
def musical_chairs(N, M, K):
    # An array to store the initial position of each player
    positions = [i for i in range(M)]
    # Rotate all the players K times
    for i in range(K):
        for j in range(M):
            # Calculate the next position for each player
            positions[j] = (positions[j] + 1) % N
    # An array of lists to store the final position of each player
    result = [[] for i in range(N)]
    for i in range(M):
        result[positions[i]].append(i)
    # Return the final positions
    return result
  
  
# Call the musical_chairs function and store the result in the ans variable
ans = musical_chairs(5, 10, 7)
# Print the final position of each player
for vect in ans:
    for i in vect:
        print(i, end =' ')
    print()


Javascript




function musical_chairs(N, M, K) {
    // An array to store the initial position of each player
    let positions = Array.from({length: M}, (_, i) => i);
  
    // Rotate all the players K times
    for (let i = 0; i < K; i++) {
        for (let j = 0; j < M; j++) {
            // Calculate the next position for each player
            positions[j] = (positions[j] + 1) % N;
        }
    }
  
    // An array of arrays to store the final position of each player
    let result = Array.from({length: N}, () => []);
  
    for (let i = 0; i < M; i++) {
        result[positions[i]].push(i);
    }
  
    // Return the final positions
    return result;
}
  
// Example usage
let result = musical_chairs(5, 10, 7);
console.log(result);


C#




// C# code to implement the above approach
using System;
using System.Collections.Generic;
  
public class Program {
    public static List<List<int> >
    MusicalChairs(int N, int M, int K)
    {
        // An array to store the initial position of each
        // player
        int[] positions = new int[M];
        for (int i = 0; i < M; ++i) {
            positions[i] = i;
        }
  
        // Rotate all the players K times
        for (int i = 0; i < K; ++i) {
            for (int j = 0; j < M; ++j) {
                // Calculate the next position for each
                // player
                positions[j] = (positions[j] + 1) % N;
            }
        }
  
        // An array of lists to store the final position of
        // each player
        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 the final positions
        return result;
    }
  
    public static void Main()
    {
        // Call the MusicalChairs function and store the
        // result in the ans variable
        List<List<int> > ans = MusicalChairs(5, 10, 7);
  
        // Print the final position of each player
        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++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Define the function musicalChairs that
// takes in the number of chairs (N),
// number of people (M), and the number
// of times to rotate the chairs (K)
vector<vector<int> > musicalChairs(int N, int M, int K)
{
  
    // Calculate the number of iterations
    // as the remainder of K divided by N
    int numIterations = K % N;
  
    // Create a vector 'positions' of
    // size M to store the positions
    // of each person
    vector<int> positions(M);
  
    // Fill in the position of each person
    // in the vector
    for (int i = 0; i < M; ++i) {
  
        // The position of the person is
        // calculated as the sum of their
        // initial position and the number
        // of iterations, modulo N
        positions[i] = (i + numIterations) % N;
    }
  
    // Create a 2D vector 'result' of
    // size N to store the positions
    // of the people
    vector<vector<int> > result(N);
  
    // Fill in the 2D vector 'result'
    // with the positions of the people
    for (int i = 0; i < M; ++i) {
        result[positions[i]].push_back(i);
    }
  
    // Return the final result
    return result;
}
  
// Driver's code
int main()
{
  
    // Call the musicalChairs function and
    // store the result in the vector 'ans'
    vector<vector<int> > ans = musicalChairs(5, 10, 7);
  
    // Print out the result
    for (auto vect : ans) {
        for (auto i : vect) {
            cout << i << " ";
        }
        cout << '\n';
    }
  
    // Return 0 to indicate the program
    // has finished successfully
    return 0;
}


Java




// Java code to implement the above approach
import java.util.*;
  
public class Main {
  
    // Define the function musicalChairs that takes in the
    // number of chairs (N), number of people (M), and the
    // number of times to rotate the chairs (K)
    public static List<List<Integer> >
    musicalChairs(int N, int M, int K)
    {
        // Calculate the number of iterations as the
        // remainder of K divided by N
        int numIterations = K % N;
  
        // Create a list 'positions' of size M to store the
        // positions of each person
        List<Integer> positions = new ArrayList<>();
        for (int i = 0; i < M; ++i) {
            positions.add(i);
        }
  
        // Fill in the position of each person in the list
        for (int i = 0; i < M; ++i) {
            // The position of the person is calculated as
            // the sum of their initial position and the
            // number of iterations, modulo N
            positions.set(
                i, (positions.get(i) + numIterations) % N);
        }
  
        // Create a 2D list 'result' of size N to store the
        // positions of the people
        List<List<Integer> > result = new ArrayList<>();
        for (int i = 0; i < N; ++i) {
            result.add(new ArrayList<>());
        }
  
        // Fill in the 2D list 'result' with the positions
        // of the people
        for (int i = 0; i < M; ++i) {
            result.get(positions.get(i)).add(i);
        }
  
        // Return the final result
        return result;
    }
  
    public static void main(String[] args)
    {
        // Call the musical_chairs function and store the
        // result in the variable 'ans'
        List<List<Integer> > ans = musicalChairs(5, 10, 7);
  
        // Print the final position of each player
        for (List<Integer> list : ans) {
            for (int i : list) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}


Python3




# Python code to implement the same
def musical_chairs(N, M, K):
    # Calculate the number of iterations as the remainder of K divided by N
    num_iterations = K % N
  
    # Create a list 'positions' of size M to store the positions of each person
    positions = [0] * M
  
    # Fill in the position of each person in the list
    for i in range(M):
        # The position of the person is calculated as the sum of their initial position and the number of iterations, modulo N
        positions[i] = (i + num_iterations) % N
  
    # Create a 2D list 'result' of size N to store the positions of the people
    result = [[] for _ in range(N)]
  
    # Fill in the 2D list 'result' with the positions of the people
    for i in range(M):
        result[positions[i]].append(i)
  
    # Return the final result
    return result
  
  
# Call the musical_chairs function and store the result in the variable 'ans'
ans = musical_chairs(5, 10, 7)
  
# Print the final position of each player
for vect in ans:
    for i in vect:
        print(i, end =" ")
    print()


Javascript




function musical_chairs(N, M, K) {
    // Calculate the number of iterations as the remainder of K divided by N
    let numIterations = K % N;
  
    // Create an array 'positions' of size M to store the positions of each person
    let positions = new Array(M);
  
    // Fill in the position of each person in the array
    for (let i = 0; i < M; ++i) {
        // The position of the person is calculated as the sum of their initial position and the number of iterations, modulo N
        positions[i] = (i + numIterations) % N;
    }
  
    // Create a 2D array 'result' of size N to store the positions of the people
    let result = new Array(N);
  
    // Fill in the 2D array 'result' with the positions of the people
    for (let i = 0; i < N; ++i) {
        result[i] = [];
    }
  
    for (let i = 0; i < M; ++i) {
        result[positions[i]].push(i);
    }
  
    // Return the final result
    return result;
}
  
// Example usage:
let ans = musical_chairs(5, 10, 7);
  
// Print the final position of each player
for (let i = 0; i < ans.length; ++i) {
    console.log(ans[i]);
}


C#




// C# code to implement the same
using System;
using System.Collections.Generic;
  
class Program {
    static List<List<int> > MusicalChairs(int N, int M,
                                          int K)
    {
        // Calculate the number of iterations as the
        // remainder of K divided by N
        int num_iterations = K % N;
  
        // Create a list 'positions' of size M to store the
        // positions of each person
        List<int> positions = new List<int>(M);
  
        // Fill in the position of each person in the list
        for (int i = 0; i < M; ++i) {
            // The position of the person is calculated as
            // the sum of their initial position and the
            // number of iterations, modulo N
            positions.Add((i + num_iterations) % N);
        }
  
        // Create a 2D list 'result' of size N to store the
        // positions of the people
        List<List<int> > result = new List<List<int> >(N);
        for (int i = 0; i < N; ++i) {
            result.Add(new List<int>());
        }
  
        // Fill in the 2D list 'result' with the positions
        // of the people
        for (int i = 0; i < M; ++i) {
            result[positions[i]].Add(i);
        }
  
        // Return the final result
        return result;
    }
  
    // Driver's code
    static void Main(string[] args)
    {
        // Call the MusicalChairs function and store the
        // result in the list 'ans'
        List<List<int> > ans = MusicalChairs(5, 10, 7);
  
        // Print out the result
        foreach(List<int> vect in ans)
        {
            foreach(int i in vect)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
        }
  
        // Wait for user input before closing the console
        // window
        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
Previous
Next
Share your thoughts in the comments
Similar Reads