Loading...
Loading...
There are n coupons numbered 1 to n, sitting in a circle. Counting starts at coupon 1, which is counted as 1. The k-th coupon counted is removed, and counting continues from the coupon right after it. This repeats until one coupon is left, the winner. Print the coupons in the order they are removed, then the winner.
Removed: <numbers separated by spaces> (or Removed: none when n is 1) and then Winner: <number>.Example
Input
5 2
Output
Removed: 2 4 1 5 Winner: 3
Counting 1, 2 removes 2. Then 3, 4 removes 4. Then 5, 1 removes 1. Then 3, 5 removes 5. Coupon 3 is left.
Try the problem first. Open one hint at a time.
Keep the coupons in an ArrayList and a current index idx that starts at 0.
The next index to remove is (idx + k - 1) % list.size().
After remove(idx) the next coupon slides into idx, so idx already points at where counting continues.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor