Josephus Winner
JavaScript
Hard
3 views
Problem Description
n people are standing in a circle (1..n). Starting from 1, every k-th person is removed. Print the winner position.
Input Format
One line: n k.
Output Format
One integer winner.
Official Solution
const fs=require('fs');const p=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!p[0])process.exit(0);const n=Number(p[0]);const k=Number(p[1]);let res=0;for(let i=1;i<=n;i++)res=(res+k)%i;process.stdout.write(String(res+1));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!