Skip to content
Related Articles

Related Articles

Improve Article

Order of removal in Josephus problem in O(N logN)

  • Last Updated : 20 Jul, 2021

Given N children standing in a circle waiting to be executed, and a number K, which indicates that K-1 children are skipped in the clockwise direction, and the Kth child is killed in the circle, and then execution of (K+1)th child begins, the task is to print the child who will get killed in the ith move if the execution starts from the first child.

Note: The last child is considered dead at the end by default.

Examples:

Input: N = 5, K = 2
Output: 3 1 5 2 4
Explanation: 
Initially, the arrangement is {1, 2, 3, 4, 5} and the operations performed are:

  1. Counting from 1, the Kth child is 3.  So the 3rd child gets killed. After that, the children left to be executed are {1, 2, 4, 5}, and then execution of child 4 begins.
  2. Counting from 4, the Kth child is 1.  So the first child gets killed. After that, the children left to be executed are {2, 4, 5}, and then execution of child 2 begins.
  3. Counting from 2, the Kth child is 5. So the fifth child gets killed. After that, the children left to be executed are {2, 4}, and then execution of child 2 begins.
  4. Counting from 2, the Kth child is 2.  So the second child gets killed. After that, the child left to be executed is 2, and then execution of child 4 begins.
  5. Finally, child 4 is the only remaining child. So the child will be killed.

Input: N = 7, K = 2
Output: 3 6 2 7 5 1 4



Naive Approach: The simplest idea is to use a vector to store the position of the remaining children. Then iterate while the size of the vector is greater than 1, and in each iteration erase the desired position from the vector.

Time Complexity: O(N2)
Auxiliary Space: O(N)

Efficient Approach: The above approach can be optimized using an ordered set. Follow the steps below to solve the problem:

  • Initialize an ordered set, say V, and insert the elements in the range [1, N] into V.
  • Initialize a variable, say pos as 0, to store the index of the removed element.
  • Iterate until the size of the set, V is greater than 1, and perform the following steps:
    • Store the size of the set in a variable, say X.
    • Update the value of pos to (pos + K) % X.
    • Print the element pointed by pos in V and then erase it.
    • Update pos to pos%V.size().
  • Finally, after completing the above steps, print the last element stored at the beginning of set V.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
  
// Header files, namespaces to use
// ordered set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
  
#define ordered_set                              \
    tree<int, null_type, less<int>, rb_tree_tag, \
         tree_order_statistics_node_update>
  
// Function to find the child who
// will get killed in the ith step
void orderOfExecution(int N, int K)
{
  
    // Create an ordered set
    ordered_set V;
  
    // Push elements in the range
    // [1, N] in the set
    for (int i = 1; i <= N; ++i)
        V.insert(i);
  
    // Stores the position to be removed
    int pos = 0;
  
    // Iterate until the size of the set
    // is greater than 1
    while (V.size() > 1) {
  
        // Update the position
        pos = (pos + K) % (int)V.size();
  
        // Print the removed element
        cout << *(V.find_by_order(pos)) << ' ';
  
        // Erase it from the ordered set
        V.erase(*(V.find_by_order(pos)));
  
        // Update position
        pos %= (int)V.size();
    }
  
    // Print the first element of the set
    cout << *(V.find_by_order(0));
}
  
// Driver Code
int main()
{
    // Given input
    int N = 5, K = 2;
  
    // Function Call
    orderOfExecution(N, K);
  
    return 0;
}
Output
3 1 5 2 4

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :