Problem Statement
A permutation of size N is a rearrangement of the sequence 1,2,...,N such that no number is discarded or duplicated. A permutation can always be decomposed into a collection of disjoint cycles of the form (x1 x2 ... xk), which means that the number x1 is in position x2, the number x2 is in position x3, and so on, up to the number xk, which is in position x1. Every number in the permutation appears in exactly one such cycle. For example, the permutation 3,5,4,1,2 can be decomposed into the cycles (1 4 3) and (2 5), because
- the number 1 is in position 4,
- the number 4 is in position 3,
- the number 3 is in position 1,
- the number 2 is in position 5, and
- the number 5 is in position 2.
Note that positions in this definition start at one, not zero.
Create a class PermutationCycles containing a method cycles that
takes an
Definition
- Class:
- PermutationCycles
- Method:
- cycles
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int cycles(int[] permutation)
- (be sure your method is public)
Notes
- Any rotation of a cycle counts as the same cycle. For example, the cycle (1 4 3) can also be written (4 3 1) or (3 1 4).
Constraints
- permutation contains between 1 and 20 elements, inclusive.
- Every integer in permutation is between 1 and the number of elements in permutation, inclusive.
- No integer appears more than once in permutation.
Examples
{ 3, 5, 4, 1, 2 }
Returns: 2
The example above.
{ 1, 2, 3, 5, 6, 4 }
Returns: 4
The cycles are (1), (2), (3), and (4 6 5).
{ 5, 4, 3, 2, 1 }
Returns: 3
The cycles are (1 5), (2 4), and (3).
{ 1 }
Returns: 1
{ 17, 19, 7, 3, 8, 6, 11, 5, 14, 20, 1, 2, 16, 9, 15, 4, 10, 18, 13, 12 }
Returns: 6
{ 1, 2, 3, 4, 5 }
Returns: 5
{ 3, 4, 1, 6, 9, 2, 5, 7, 8}
Returns: 3
{ 18, 15, 6, 19, 1, 11, 3, 14, 2, 17, 20, 13, 4, 12, 5, 8, 7, 16, 10, 9 }
Returns: 1
{ 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 }
Returns: 2
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
Returns: 20
{ 4, 3, 2, 7, 6, 5, 10, 9, 8, 1 }
Returns: 4
{ 2, 1 }
Returns: 1
{ 1, 2 }
Returns: 2
{ 15, 14, 13, 12, 11, 1, 2, 3, 4, 5, 10, 9, 8, 7, 6 }
Returns: 5
{ 5, 6, 7, 8, 9, 10, 11, 1, 2, 3, 4 }
Returns: 1
{ 1, 9, 6, 5, 7, 2, 10, 12, 11, 3, 4, 8 }
Returns: 3
{ 6, 3, 9, 7, 4, 10, 5, 2, 8, 1 }
Returns: 3
{ 4, 2, 6, 8, 10, 7, 1, 3, 9, 5 }
Returns: 4
{ 2, 3, 5, 7, 11, 13, 17, 19, 4, 6, 9, 10, 14, 15, 8, 12, 18, 20, 16, 1 }
Returns: 2
{ 5, 4, 10, 7, 3, 6, 2, 13, 11, 1, 12, 9, 8 }
Returns: 5
{ 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 }
Returns: 1
{ 1, 2, 3 }
Returns: 3
{ 4, 5, 3, 2, 1, 6 }
Returns: 3
{ 2, 3, 4, 5, 1, 6 }
Returns: 2
{ 1, 2, 4, 3, 7, 6, 9, 5, 10, 8 }
Returns: 5
{ 2, 3, 4, 5, 1 }
Returns: 1
{ 2, 3, 4, 1 }
Returns: 1
{ 1, 2, 3, 5, 11, 6, 4, 7, 10, 8, 9, 12 }
Returns: 6
{ 4, 5, 1, 2, 3 }
Returns: 1
{ 5, 4, 3, 2, 1 }
Returns: 3
{ 1, 2, 3 }
Returns: 3
{ 4, 5, 3, 2, 1, 6 }
Returns: 3
{ 2, 3, 4, 5, 1, 6 }
Returns: 2
{ 1, 2, 4, 3, 7, 6, 9, 5, 10, 8 }
Returns: 5
{ 2, 3, 4, 5, 1 }
Returns: 1
{ 2, 3, 4, 1 }
Returns: 1
{ 1, 2, 3, 5, 11, 6, 4, 7, 10, 8, 9, 12 }
Returns: 6
{ 4, 5, 1, 2, 3 }
Returns: 1
{ 5, 4, 3, 2, 1 }
Returns: 3