Skip to main content

Find the Winner of the Circular Game Solution using Recursion | Josephus Problem

Find the Winner of the Circular Game Solution using Recursion with Explanation

Find the Winner of the Circular Game Solution using Recursion with Explanation | Josephus Problem
 

Problem Description :

There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

The rules of the game are as follows:

  1. Start at the 1st friend.
  2. Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
  3. The last friend you counted leaves the circle and loses the game.
  4. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
  5. Else, the last friend in the circle wins the game.

Given the number of friends, n, and an integer k, return the winner of the game.

This problem is also called Josephus Problem. To learn more about this problem

Example 1 :

Input: n = 5, k = 2
Output: 3
Explanation: Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.

Example 2 :

Input: n = 6, k = 5
Output: 1
Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
 

So, we have to delete k value until only one element remaining in list.

Here we can use many Data structure like, ArrayList, LinkedList, Queue, etc..

In solution 1, we will use ArrayList data structure for storing n values and use Recursion approach for solving problem.

Solution 1 :

class Solution {

    public int findTheWinner(int n, int k) {
        
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            list.add(i+1);
        }
       
        // Pass list, starting from 0, next k location for remove
        // List index start from 0, so We are starting from 0 and passing k-1
        return removeElements(list, 0, k-1);
    }
    
    private static int removeElements(List<Integer> list, int start, int k) {
       
        if (list.size() == 1) {
            return list.get(0);
        }
       
        start = (start+k) % list.size();
        list.remove(start);
        return removeElements(list, start, k);
    }
}

Solution Explanation :

n = 5, k = 2

  • In findTheWinner() method, create array list and initialize with 1 to n values. 
  • Call removeElements() method with list, start, and k parameters.

list = [1, 2, 3, 4, 5], n = 5, k = 1, start = 0

  • removeElements([1, 2, 3, 4, 5], 0, 1)
    • list.size() == 1 | 5 == 1 becomes false
    • start = (0+1) % 5 = 1
    • list.remove(1) | remove element from index 1, remove 2 from list
  • removeElements([1, 3, 4, 5], 1, 1)
    • 4 == 1 becomes false
    • start = (1+1) % 4 = 2
    • list.remove(2) | remove element from index 2, remove 4 from list
  • removeElements([1, 3, 5], 2, 1)
    • 3 == 1 becomes false
    • start = (2+1) % 3 = 0
    • list.remove(0) | remove element from index 0, remove 1 from list
  • removeElements([3, 5], 0, 1)
    • 2 == 1 becomes false
    • start = (0+1) % 2 =1
    • list.remove(1) | remove element from index 1, remove 5 from list
  • removeElements([3], 1, 1)
    • 1 == 1 becomes true
      • return list.get(0) | return 3


If you have any query on above solution, you can freely contact me or comment down.

Happy coding...

RECOMMENDED ARTICLES :

 


Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last