Skip to content

Commit

Permalink
Create JosephusTrap.java
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithasudhakar authored Nov 24, 2024
1 parent 201ac9c commit 7d02c00
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Math/JosephusTrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Recursive Approach

import java.util.Scanner;

class JosephusTrap {
public static int findTheWinner(int n, int k) {
if(n == 1){
return n;
}
return (findTheWinner(n-1,k) + k-1) % n+1;
}

public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
findTheWinner(n, k);
}
}

0 comments on commit 7d02c00

Please sign in to comment.