Implementing the Josephus Problem Using Circular Linked Lists in Java

 

Implementing the Josephus Problem Using Circular Linked Lists in Java

Problem Statement:

The Josephus problem involves a group of N people standing in a circle waiting to be executed. Starting from a predetermined position in the circle, each Kth person is executed until only one person remains. The task is to find the position of the last person who manages to survive.


Solution:

To solve the Josephus problem, we can use a circular linked list and traverse the list, deleting every Kth node until only one node remains. Here's how we can implement this solution in Java:


import java.util.*;


class JosephusProblem {

static int findSurvivorPosition(int numPeople, int skipCount) {

    LinkedList<Integer> list = new LinkedList<>();


    // Add elements numbered from 0 to N-1 in LinkedList

    for (int i = 0; i < numPeople; i++) {

        list.add(i);

    }


    Iterator<Integer> it = list.iterator();

    int count = 0;


    // Run this loop until there is only one element left in the list

    while (list.size() > 1) {

        while (it.hasNext()) {

            int person = it.next();

            count++;


            // If we have reached the end of the list, wrap the iterator back to the beginning

            if (!it.hasNext()) {

                it = list.iterator();

            }


            // If we have found the Kth person, remove them from the list

            if (count == skipCount) {

                it.remove();

                count = 0;

            }

        }

    }


    // Return the position of the survivor, which is the only element left in the list

    return list.getFirst();

}


public static void main(String[] args) {

    int numPeople = 7;

    int skipCount = 3;


    int survivorPosition = findSurvivorPosition(numPeople, skipCount);

    System.out.println("The survivor is at position " + survivorPosition);

}}


In this implementation, we use a while loop to iterate through the linked list and a nested while loop to find the Kth person. We then use the iterator's remove() method to remove the Kth person from the list. When we reach the end of the list, we use the iterator's hasNext() method to check if we have reached the end of the list and wrap the iterator back to the beginning using the iterator() method.


By using a circular linked list and wrapping the iterator back to the beginning of the list, we avoid the need to check if we have reached the end of the list and simplify the implementation.


Here's the implementation:


import java.util.LinkedList;

import java.util.ListIterator;


class JosephusProblem {


    public static int findSafePosition(int n, int k) {

        // Create a circular linked list with elements numbered from 0 to n-1

        LinkedList<Integer> linkedList = new LinkedList<>();

        for (int i = 0; i < n; i++) {

            linkedList.add(i);

        }


        // Initialize iterator at the beginning of the linked list

        ListIterator<Integer> iterator = linkedList.listIterator();


        // Remove every kth person until only one person is left

        while (linkedList.size() > 1) {

            // Iterate to the kth person in the linked list

            for (int i = 0; i < k - 1; i++) {

                // If we've reached the end of the linked list, wrap around to the beginning

                if (!iterator.hasNext()) {

                    iterator = linkedList.listIterator();

                }

                iterator.next();

            }

            // Remove the kth person from the linked list

            iterator.remove();


            // If we've removed the last person in the linked list, wrap around to the beginning

            if (!iterator.hasNext()) {

                iterator = linkedList.listIterator();

            }

        }


        // The last remaining person in the linked list is the safe position

        return linkedList.getFirst();

    }


    public static void main(String[] args) {

        int n = 5;

        int k = 2;

        int safePosition = findSafePosition(n, k);

        System.out.println("Safe position for n=" + n + " and k=" + k + " is " + safePosition);


        n = 7;

        k = 3;

        safePosition = findSafePosition(n, k);

        System.out.println("Safe position for n=" + n + " and k=" + k + " is " + safePosition);

    }

}


In this implementation, we use a for loop to iterate to the kth person in the linked list, instead of a while loop with a nested if statement. If we've reached the end of the linked list, we simply wrap around to the beginning by resetting the iterator to the beginning of the linked list.


We've also renamed the josephus method to findSafePosition to better reflect what the method does.


Overall, this implementation is more concise and easier to read than the previous implementation.


Previous Post Next Post