Given an even integer n
, you start with a permutation perm
of length n
where perm[i] = i
for 0 ≤ i < n
. You then perform the following operation repeatedly to generate a new array arr
from perm
:
i
is even, then arr[i] = perm[i / 2]
i
is odd, then arr[i] = perm[n / 2 + (i - 1) / 2]
After each operation, perm
becomes arr
. The task is to determine the minimum number of operations required to return perm
back to its original order (that is, perm[i] = i
for all i
).
Constraints:
2 ≤ n ≤ 1000
n
is evenAt first glance, the problem seems to require simulating the permutation repeatedly until it cycles back to the starting order. A brute-force approach would be to keep applying the operation, checking after each step whether the array has returned to its original state.
However, because n
can be as large as 1000, simulating the entire array for each operation could be inefficient. We should look for patterns or properties in the way elements are shuffled to optimize our solution.
Notably, the operation is a deterministic mapping: each index moves to a new position based on whether it's even or odd. This hints at a possible cycle structure or mathematical pattern that can be exploited.
Let's break down the steps to solve the problem efficiently:
pos = 1
.
pos
is even, new pos = pos / 2
pos
is odd, new pos = n / 2 + (pos - 1) / 2
pos
returns to 1, counting the number of steps taken.
This approach is efficient because it avoids simulating the entire array and only tracks one index through its cycle.
Let's walk through the process for n = 6
:
perm = [0,1,2,3,4,5]
pos = 1
(the index of value 1)pos = 1
is odd ⇒ pos = 6/2 + (1-1)/2 = 3
pos = 3
is odd ⇒ pos = 6/2 + (3-1)/2 = 4
pos = 4
is even ⇒ pos = 4/2 = 2
pos = 2
is even ⇒ pos = 2/2 = 1
4
.
O(n)
per operation, and up to O(n)
operations, leading to O(n^2)
time and O(n)
space.
O(1)
, and in the worst case, we do up to O(n)
operations (since the cycle length can be up to n-1
). Thus, overall time complexity is O(n)
and space complexity is O(1)
.
The key insight is to realize that the permutation operation forms cycles, and the permutation is restored when all cycles complete. By tracking the position of a single element (like index 1), we can efficiently compute the minimum number of operations needed without simulating the entire array. This approach is both elegant and efficient, reducing the problem to simple arithmetic operations and a single loop.
class Solution:
def reinitializePermutation(self, n: int) -> int:
pos = 1
steps = 0
while True:
if pos % 2 == 0:
pos = pos // 2
else:
pos = n // 2 + (pos - 1) // 2
steps += 1
if pos == 1:
return steps
class Solution {
public:
int reinitializePermutation(int n) {
int pos = 1;
int steps = 0;
do {
if (pos % 2 == 0)
pos = pos / 2;
else
pos = n / 2 + (pos - 1) / 2;
steps++;
} while (pos != 1);
return steps;
}
};
class Solution {
public int reinitializePermutation(int n) {
int pos = 1, steps = 0;
do {
if (pos % 2 == 0)
pos = pos / 2;
else
pos = n / 2 + (pos - 1) / 2;
steps++;
} while (pos != 1);
return steps;
}
}
var reinitializePermutation = function(n) {
let pos = 1, steps = 0;
do {
if (pos % 2 === 0)
pos = pos / 2;
else
pos = Math.floor(n / 2 + (pos - 1) / 2);
steps++;
} while (pos !== 1);
return steps;
};