Question

Assume N people have decided to elect a leader by arranging themselves in a circle and...

Assume N people have decided to elect a leader by arranging themselves in a circle and eliminating every Mth person around the circle, closing ranks as each person drops out. Find which person will be the last one remaining (with rank 1). HINT: Assume the input is a circular linked list with N nodes and each node has a number (range 1 to N) associated with it. The head node has number 1 as data. Although. It will be easier to implement the solution using a circular linked list, you are free to use Array if you see appropriate.

Homework Answers

Answer #1

The problem has following recursive structure.

  josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1
  josephus(1, k) = 1

After the first person (kth from beginning) is dropped out, n-1 persons are left. So we call josephus(n – 1, k) to get the position with n-1 persons.

Here is the pseudocode to do the same

PSEUDOCODE

int josephus(int N, int M)

{

  if (N == 1)

    return 1;

  else

    return (josephus(N - 1, M) + M-1) % N + 1;

}

CODE

public class Main {

public static class Node

{

public int data ;

public Node next;

public Node( int data )

{

this.data = data;

}

}

public static void getJosephusPosition(int m, int n)

{

Node head = new Node(1);

Node prev = head;

for(int i = 2; i <= n; i++)

{

prev.next = new Node(i);

prev = prev.next;

}

prev.next = head;

Node ptr1 = head, ptr2 = head;

while(ptr1.next != ptr1)

{

int count = 1;

while(count != m)

{

ptr2 = ptr1;

ptr1 = ptr1.next;

count++;

}

ptr2.next = ptr1.next;

ptr1 = ptr2.next;

}

System.out.println("Elected position = " + ptr1.data);

}

public static void main(String args[])

{

int n = 20, m = 3;

getJosephusPosition(m, n);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
QUESTION 1 1. Brianna is trying to increase her chances of being promoted to vice president...
QUESTION 1 1. Brianna is trying to increase her chances of being promoted to vice president by working to build good work relationships with other managers outside her own department. Brianna's behavior should be viewed as dysfunctional politics. functional politics. coercive power. functional influence. 2 points QUESTION 2 1. The Gingerbread Factory has a separate unit that makes their chocolate crunch cookies and another unit that is completely responsible for all operations in producing their ginger snap cookies. The Gingerbread...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described...
Gender Bias in the Executive Suite Worldwide The Grant Thornton International Business Report (IBR) has described itself as "a quarterly survey of business leaders from across the globe … surveying 11,500 businesses in 40 economies across the globe on an annual basis." 1 According to the 2011 IBR, the Asia Pacific region had a higher percentage (27 percent) of female chief executive officers (CEOs) than Europe and North America. Japan is the only Asia Pacific region exception. The report further...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • What is the temperature of N2 gas if the average speed (actually the root-mean-square speed) of...
    asked 1 minute ago
  • Question One: Basic security concepts and terminology                         (2 marks) Computer security is the protection of...
    asked 14 minutes ago
  • In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...
    asked 22 minutes ago
  • the determination of aspirin in commercial preparations experment explain why the FeCl3-KCl-HCl solution was ased as...
    asked 34 minutes ago
  • Describe important events and influences in the life of Wolfgang Amadeus Mozart. What styles, genres, and...
    asked 37 minutes ago
  • 3.12 Grade Statistics Write a python module "school.py" that prints school information (first 3 lines of...
    asked 38 minutes ago
  • Using python, please explain. The factorial of an integer N is the product of the integers...
    asked 42 minutes ago
  • alamoto Co. manufactures a single product that goes through two processes — mixing and cooking. The...
    asked 47 minutes ago
  • QUESTION 21 _______ is the ease of use with which network users can access the network...
    asked 52 minutes ago
  • Configure IP static routing using 6 pc's, 3 routers & 3 switches by using CISCO PACKET...
    asked 1 hour ago
  • Write a C++ program that calculates the series and parallel resistance for a group of resister...
    asked 1 hour ago
  • 1. A host computer is assigned the IP address 192.168.12.8 and a subnet mask of 255.255.255.192....
    asked 1 hour ago