Minimum Number of Permutation Operations π
Updated
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[]) β intExamples
Example 1
p = [1, 3, 2]return = 2Here 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, wherenis the length ofp1 β€ p[i] β€ npcontains every integer from 1 tonexactly 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