java.lang.Object
g1801_1900.s1806_minimum_number_of_operations_to_reinitialize_a_permutation.Solution

public class Solution extends Object
1806 - Minimum Number of Operations to Reinitialize a Permutation.<p>Medium</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> perm = [0,1] initially.</p> <p>After the 1<sup>st</sup> operation, perm = [0,1]</p> <p>So it takes only 1 operation.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 4</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> perm = [0,1,2,3] initially.</p> <p>After the 1<sup>st</sup> operation, perm = [0,2,1,3]</p> <p>After the 2<sup>nd</sup> operation, perm = [0,1,2,3]</p> <p>So it takes only 2 operations.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 6</p> <p><strong>Output:</strong> 4</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 1000</code></li> <li><code>n</code> is even.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • reinitializePermutation

      public int reinitializePermutation(int n)