Find the Period of a Perfect Interleaving

Quick Overview

Find the Period of a Perfect Interleaving. Begin with the identity permutation of even length n. In each operation create a new array: at even index i take the previous entry at i/2; at odd index i take the previous entry at n/2+(i-1)/2.

Find the Period of a Perfect Interleaving

Company: TCS

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Begin with the identity permutation of even length `n`. In each operation create a new array: at even index i take the previous entry at i/2; at odd index i take the previous entry at n/2+(i-1)/2. Replace the old permutation simultaneously. Return the least positive operation count that restores the identity. ```cpp class Solution { public: int reinitializePermutation(int n); }; ``` ### Examples **Example 1:** ```text n = 8 Output: 3 Explanation: Interior position 1 moves through 2, 4, and back to 1 modulo 7. ``` **Example 2:** ```text n = 10 Output: 6 Explanation: Position 1 follows 1 -> 2 -> 4 -> 8 -> 7 -> 5 -> 1 modulo 9. ``` ### Constraints - `2 <= n <= 1000`. - n is even. ### Clarifying Questions The update reads the old permutation for every new entry. The answer must be positive even if the first operation leaves the permutation unchanged. ```hint Inspect the structure Does each new entry read the old permutation, and must the reported restoration count be positive? ``` ### Reasoning checkpoints - Interpret the simultaneous old-to-new permutation rule without mixing movement and lookup directions. - Prove the least positive return time for the entire permutation, including `n = 2`. ### Optional extensions How would direct array simulation compare with tracking a single interior position?

Overview: Find the Period of a Perfect Interleaving. Begin with the identity permutation of even length n. In each operation create a new array: at even index i take the previous entry at i/2; at odd index i take the previous entry at n/2+(i-1)/2.

TCS logo
TCS
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Begin with the identity permutation of even length n. In each operation create a new array: at even index i take the previous entry at i/2; at odd index i take the previous entry at n/2+(i-1)/2. Replace the old permutation simultaneously. Return the least positive operation count that restores the identity.

class Solution {
public:
    int reinitializePermutation(int n);
};

Examples

Example 1:

n = 8
Output: 3
Explanation: Interior position 1 moves through 2, 4, and back to 1 modulo 7.

Example 2:

n = 10
Output: 6
Explanation: Position 1 follows 1 -> 2 -> 4 -> 8 -> 7 -> 5 -> 1 modulo 9.

Constraints

  • 2 <= n <= 1000 .
  • n is even.

Clarifying Questions Guidance

The update reads the old permutation for every new entry. The answer must be positive even if the first operation leaves the permutation unchanged.

Reasoning checkpoints

  • Interpret the simultaneous old-to-new permutation rule without mixing movement and lookup directions.
  • Prove the least positive return time for the entire permutation, including n = 2 .

Optional extensions

How would direct array simulation compare with tracking a single interior position?