Search for a command to run...
Jack, a sports teacher at St. Patrick’s School, created a game for his students.
He lined up N students, each wearing a jersey numbered 1 to N, in increasing order.
In front of each student, there is a board showing a unique integer from 1 to N. These numbers form a permutation array B[].
After every drum beat, each student moves to the position indicated by the number on their current board.
This process keeps repeating until all students return to their original order (1 to N).
Your task is to find how many drum beats are required for the students to return to the original arrangement.
Input
5
2 3 1 5 4Output
6Students move according to the board numbers after each drum beat.
The positions keep changing following the permutation.
After 6 beats, the arrangement becomes [1, 2, 3, 4, 5] again.
Therefore, the answer is 6.
Example 1
8
2 1 4 3 6 5 8 7
2Example 2
6
3 1 6 2 5 4
4Example 3
5
2 3 1 5 4
68 2 1 4 3 6 5 8 7
2