Given a number n, generate all the possible cyclic permutations of the number.
Examples:Â
Input: n = 123
Output: 123 312 231
Explanation: For 123 there are 3 cyclic permutations possible.Input: n = 5674
Output: 5674 4567 7456 6745
Explanation: For 5674 there are 4 cyclic permutations possible.
Try It Yourself
[Mathematical Rotation Approach] - O(n * d) Time and O(n) SpaceÂ
The idea is to repeatedly move the last digit to the front. Each rotation does the following
- Extract the last digit using modulo.
- Remove it using division.
- Reconstruct the new number by placing that digit at the most significant position using powers of 10 based on the digit count.
- Count total digits in number using repeated division.
- Handle n = 0 as edge case.
- Start with original number and push into result vector.
- Extract last digit (
rem = num % 10) and remaining number (div = num / 10). - Form new number:Â
(10^(n-1)) * rem + div. - Repeat until we return to original number.
// Program to generate all cyclic permutations
// of number
#include <bits/stdc++.h>
using namespace std;
// Function to count the total number of digits
// in a number.
int countdigits(int n)
{
int count = 0;
while (n) {
count++;
n = n / 10;
}
return count;
}
// Function to generate all cyclic permutations
// of a number and return them as a vector
vector<int> cyclicPermutations(int n)
{
vector<int> result;
int num = n;
int n_digits = countdigits(n);
// Handle the case when n = 0
if (n == 0) {
result.push_back(0);
return result;
}
do {
result.push_back(num);
// Following three lines generates a
// circular permutation of a number.
int rem = num % 10;
int div = num / 10;
num = (pow(10, n_digits - 1)) * rem + div;
} while (num != n);
return result;
}
// Driver Program
int main()
{
int n = 5674;
vector<int> perms = cyclicPermutations(n);
cout << "Cyclic permutations of " << n << " are:" << endl;
for (int perm : perms) {
cout << perm << endl;
}
return 0;
}
import java.util.*;
class GFG {
// Function to count the total number of digits
// in a number.
static int countdigits(int n) {
int count = 0;
while (n != 0) {
count++;
n = n / 10;
}
return count;
}
// Function to generate all cyclic permutations
// of a number and return them as a vector
static ArrayList<Integer> cyclicPermutations(int n) {
ArrayList<Integer> result = new ArrayList<>();
int num = n;
int n_digits = countdigits(n);
// Handle the case when n = 0
if (n == 0) {
result.add(0);
return result;
}
do {
result.add(num);
// Following three lines generates a
// circular permutation of a number.
int rem = num % 10;
int div = num / 10;
num = (int)(Math.pow(10, n_digits - 1)) * rem + div;
} while (num != n);
return result;
}
// Driver Program
public static void main(String[] args) {
int n = 5674;
ArrayList<Integer> perms = cyclicPermutations(n);
System.out.println("Cyclic permutations of " + n + " are:");
for (int perm : perms) {
System.out.println(perm);
}
}
}
# Function to count the total number of digits
# in a number.
def countdigits(n):
count = 0
while n != 0:
count += 1
n = n // 10
return count
# Function to generate all cyclic permutations
# of a number and return them as a vector
def cyclicPermutations(n):
result = []
num = n
n_digits = countdigits(n)
# Handle the case when n = 0
if n == 0:
result.append(0)
return result
while True:
result.append(num)
# Following three lines generates a
# circular permutation of a number.
rem = num % 10
div = num // 10
num = (10 ** (n_digits - 1)) * rem + div
if num == n:
break
return result
# Driver Program
if __name__ == "__main__":
n = 5674
perms = cyclicPermutations(n)
print(f"Cyclic permutations of {n} are:")
for perm in perms:
print(perm)
using System;
using System.Collections.Generic;
class GFG {
// Function to count the total number of digits
// in a number.
static int countdigits(int n) {
int count = 0;
while (n != 0) {
count++;
n = n / 10;
}
return count;
}
// Function to generate all cyclic permutations
// of a number and return them as a vector
static List<int> cyclicPermutations(int n) {
List<int> result = new List<int>();
int num = n;
int n_digits = countdigits(n);
// Handle the case when n = 0
if (n == 0) {
result.Add(0);
return result;
}
do {
result.Add(num);
// Following three lines generates a
// circular permutation of a number.
int rem = num % 10;
int div = num / 10;
num = (int)(Math.Pow(10, n_digits - 1)) * rem + div;
} while (num != n);
return result;
}
// Driver Program
static void Main(string[] args) {
int n = 5674;
List<int> perms = cyclicPermutations(n);
Console.WriteLine("Cyclic permutations of " + n + " are:");
foreach (int perm in perms) {
Console.WriteLine(perm);
}
}
}
// Function to count the total number of digits
// in a number.
function countdigits(n) {
let count = 0;
while (n !== 0) {
count++;
n = Math.floor(n / 10);
}
return count;
}
// Function to generate all cyclic permutations
// of a number and return them as a vector
function cyclicPermutations(n) {
let result = [];
let num = n;
let n_digits = countdigits(n);
// Handle the case when n = 0
if (n === 0) {
result.push(0);
return result;
}
do {
result.push(num);
// Following three lines generates a
// circular permutation of a number.
let rem = num % 10;
let div = Math.floor(num / 10);
num = Math.pow(10, n_digits - 1) * rem + div;
} while (num !== n);
return result;
}
// Driver Program
let n = 5674;
let perms = cyclicPermutations(n);
console.log("Cyclic permutations of " + n + " are:");
for (let perm of perms) {
console.log(perm);
}
Output
Cyclic permutations of 5674 are: 5674 4567 7456 6745