diff --git a/Math/JosephusTrap.java b/Math/JosephusTrap.java new file mode 100644 index 0000000..8c02acb --- /dev/null +++ b/Math/JosephusTrap.java @@ -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); + } +}