<p>You are given an <strong>even</strong> integer <code>n</code>​​​​​​. You initially have a permutation <code>perm</code> of size <code>n</code>​​ where <code>perm[i] == i</code>​ <strong>(0-indexed)</strong>​​​​.</p>

<p>In one operation, you will create a new array <code>arr</code>, and for each <code>i</code>:</p>

<ul>
	<li>If <code>i % 2 == 0</code>, then <code>arr[i] = perm[i / 2]</code>.</li>
	<li>If <code>i % 2 == 1</code>, then <code>arr[i] = perm[n / 2 + (i - 1) / 2]</code>.</li>
</ul>

<p>You will then assign <code>arr</code>​​​​ to <code>perm</code>.</p>

<p>Return <em>the minimum <strong>non-zero</strong> number of operations you need to perform on </em><code>perm</code><em> to return the permutation to its initial value.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> perm = [0,1] initially.
After the 1<sup>st</sup> operation, perm = [0,1]
So it takes only 1 operation.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> perm = [0,1,2,3] initially.
After the 1<sup>st</sup> operation, perm = [0,2,1,3]
After the 2<sup>nd</sup> operation, perm = [0,1,2,3]
So it takes only 2 operations.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 6
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 1000</code></li>
	<li><code>n</code>​​​​​​ is even.</li>
</ul>

import math
from functools import reduce


class Solution:
    def reinitializePermutation(self, n: int) -> int:
        def permute(perm):
            arr = [0] * n
            for i in range(n):
                if i % 2 == 0:
                    arr[i] = perm[i // 2]
                else:
                    arr[i] = perm[n // 2 + (i - 1) // 2]
            return arr

        def find_cycles(n):
            visited = [False] * n
            cycles = []

            for start in range(n):
                if not visited[start]:
                    length = 0
                    x = start
                    while not visited[x]:
                        visited[x] = True
                        x = perm_map[x]
                        length += 1
                    cycles.append(length)
            return cycles

        # Create the initial permutation map
        perm_map = list(range(n))
        perm = permute(perm_map)
        steps = 0

        while perm != list(range(n)):
            perm_map = perm
            perm = permute(perm_map)
            steps += 1

        # Find all cycle lengths
        cycles = find_cycles(n)

        # Calculate LCM of all cycle lengths
        lcm = lambda x, y: x * y // math.gcd(x, y)
        return reduce(lcm, cycles, 1)


# Driver code for testing
def test_solution():
    sol = Solution()

    # Test case 1
    print(f"Input: n = 2")
    print(f"Output: {sol.reinitializePermutation(2)}")
    # Expected: 1

    # Test case 2
    print(f"Input: n = 4")
    print(f"Output: {sol.reinitializePermutation(4)}")
    # Expected: 2

    # Test case 3
    print(f"Input: n = 6")
    print(f"Output: {sol.reinitializePermutation(6)}")
    # Expected: 4

    # Additional test cases
    print(f"Input: n = 8")
    print(f"Output: {sol.reinitializePermutation(8)}")
    # Expected: 8 (based on detailed calculations)


# Run the driver code
test_solution()


294