Answered step by step
Verified Expert Solution
Question
1 Approved Answer
can you please help me write a pseudocode for the following code. the code is Java / Java program for Josephus problem import java.util.*; //
can you please help me write a pseudocode for the following code.
the code is Java
/ Java program for Josephus problem import java.util.*; // Josephus class class Josephus { public Listlist; public List order; Josephus(int p){ list = new ArrayList<>(); for(int i=1;i<=p;i++) list.add(i); order = new ArrayList<>(); } // Recursive function public int josephus(int start, int k) { // If size of list is one // then return its value if (list.size() == 1) { return list.get(0); } // Counting kth person and // check so that it don't go out of bound start = (start + k) % list.size(); //adding order of survivor order.add(list.get(start)); // Removing the sth person list.remove(start); // Calling recursive function again until only one // person left Start is now the position of previous // person who is killed Ex. if person at 1-index // killed then person at 2-index shifted to 1-index // and counting start from here return josephus(start, k); } public void display(){ order.add(list.get(0)); for(Integer i:order){ System.out.print(i+" "); } } } // Main class class Main{ // Main function public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Number of people to be executed System.out.print("Enter the total number of persons to be executed:"); int p=sc.nextInt(); System.out.print(" Enter the starting person to be executed:"); // Starting person int k=sc.nextInt(); System.out.print(" Enter the number of persons to be skipped:"); // Number of person to be skipped int s = sc.nextInt(); // Initialising Josephus object Josephus jos = new Josephus(p); // Calling function with starting position at // k-index so that sth person will be killed // Storing the safe position int safePosition = jos.josephus(k-1, s); //Order in which person were executed System.out.print(" order of victims :"); jos.display(); // Printing the result System.out.println(" survivor : " + safePosition); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started