FastPrepMinimum Number of Permutation Operations πŸ‘‘
Problems/Minimum Number of Permutation Operations πŸ‘‘

Minimum Number of Permutation Operations πŸ‘‘

Geneva Trading logoGeneva Trading● MediumOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

A permutation of n numbers is a list where each number from 1 to n appears exactly once. You are given a permutation p (positions are counted from 1).

One permutation operation changes an array arr of length n like this: build a new array temp where temp[i] = arr[p[i]] for every i from 1 to n, then set arr = temp.

Start with any array arr of n different values. Return the smallest number of operations (at least 1) after which arr is back to how it started. The answer can be very large, so return it modulo 10^9 + 7.

Function

minNumberOfPermutationOperations(p: int[]) β†’ int

Examples

Example 1

p = [1, 3, 2]return = 2

Here n = 3. Take any array of different values, for example arr = [7, 8, 9]. Because p = [1, 3, 2], each operation keeps the value at position 1 and swaps the values at positions 2 and 3.

After the first operation, arr is [7, 9, 8]. After the second, it is [7, 8, 9] again. So the answer is 2.

Constraints

  • 1 ≀ n ≀ 10^5, where n is the length of p
  • 1 ≀ p[i] ≀ n
  • p contains every integer from 1 to n exactly once.

Similar questions at other companies

  • Connected Components with Divisor ThresholdInMobi Β· OAInMobi interview questions
  • Can Sort Permutation in Given MovesAmazon Β· OA
  • Relative Score Graph ComponentsScale AI Β· OAScale AI interview questions
  • Count OperationsWells Fargo Β· OAWells Fargo interview questions
  • Count Routes in a Divisor GraphExpedia Β· OAExpedia interview questions
  • Reaching Points with Perfect-Square ObstaclesIMC Trading Β· OAIMC Trading interview questions
  • Connected SumAtlassian Β· OAAtlassian interview questions
  • Drawing EdgeWeRide Β· OAWeRide interview questions
  • Drawing EdgeIBM Β· OAIBM interview questions
  • Minimum Numeric Code TransformationAdobe Β· OAAdobe interview questions
See Geneva Trading hiring insights
Saved
class Solution {
    public int minNumberOfPermutationOperations(int[] p) {
        // write your code here
    }

}
Checking account…