# LeetCode: Minimum Number Of Operations To Reinitialize A Permutation Solution

1/**2 * @param {number} n3 * @return {number}4 */5var reinitializePermutation = function (n) {6 let perm = Array.from({ length: n }, (_, i) => i)7 const original = perm.join("")8 let res = 09 while (true) {10 res++11 const arr = [...perm]12 for (let i = 0; i < n; i++) {13 if (i % 2 === 0) {14 arr[i] = perm[i / 2]15 } else {16 arr[i] = perm[n / 2 + (i - 1) / 2]17 }18 }19 perm = arr20 if (perm.join("") === original) {21 break22 }23 }24 return res25}

## Comments

Loading comments...

## Tags

leetcode

array

greedy

## Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

**Fact corner:** Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

**Still hesitate?** Read HoningJS author's guide on dealing with Toptal interview process.

## Next Post

## Previous Post

LeetCode: Evaluate The Bracket Pairs Of A String

Mar 28, 2021