You are given an integer n. The numbers 1 through n are arranged in a circle in increasing order.
Starting from number 1, repeatedly skip the next remaining number, then remove the number right after it, counting only numbers that are still present. So the first number skipped is 1 and the first number removed is 2, the next skipped is 3 and the next removed is 4, and so on around the circle. Continue until every number has been removed.
Return an array containing the numbers in the order they were removed.
Examples
Example 1
Input: n = 7
Output: [2, 4, 6, 1, 5, 3, 7]
Explanation:
Round the circle 1..7: skip 1, remove 2; skip 3, remove 4; skip 5, remove 6.
Continuing from 7: skip 7, wrap around and remove 1; skip 3, remove 5.
Remaining [3, 7]: skip 7, wrap around and remove 3; skip 7, remove 7.
Example 2
Input: n = 2
Output: [2, 1]
Explanation:
Skip 1, remove 2; then skip 1 (alone in the circle), remove 1.
Constraints
1≤n≤105
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up