Given a number N, our task is to generate all the possible cyclic permutations of the number.
A cyclic permutation shifts all the elements of a set by a fixed offset. For a set with elements , , …, , a cyclic permutation of one place to the left would yield , …, , , and a cyclic permutation of one place to the right would yield , , , ….
Input : 123 Output : 123 312 231 Input : 5674 Output : 5674 4567 7456 6745
The idea is to generate next permutation of a number using below formula.
rem = num % 10; div = num / 10; num = (pow(10, n - 1)) * rem + div;
While repeating above steps, if we come back to original number, we stop and return.
5674 4567 7456 6745
This article is contributed by Vineet Joshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Generate all permutations of a string that follow given constraints
- Generate permutations with only adjacent swaps allowed
- Iterative program to generate distinct Permutations of a String
- Cyclic Number
- Number Theory | Generators of finite cyclic group under addition
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Number of palindromic permutations | Set 1
- Count the number of special permutations
- Find the number of good permutations
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Maximize a number considering permutations with values smaller than limit
- Number of unique permutations starting with 1 of a Binary String
- Generate all rotations of a number