
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate All Rotations of a Number in JavaScript
In this article, we will learn to generate all rotations of a number in JavaScript. Producing a primary common problem such as all its rotations comprises the creation of all possible permutations of the digits by rotating its digits.
Problem Statement
The goal is that if we have given a number, we need to generate all possible rotations of its digits. A rotation is defined as moving the first digit to the end of the number.
For example
Input
123
Explanation
-
123 (original number)
-
231 (first rotation: move 1 to the end)
- 312 (second rotation: move 2 to the end)
The number of rotations for a number with n digits is equal to n.
Different Approaches
The following are the different approaches to generating all rotations of a number in JavaScript ?
Using String Manipulation
The approach to generate all rotations of a number in JavaScript is to convert the number into a string, shifting its digits iteratively, and storing the results.
Following are the steps to generate all rotations of a number using string manipulation ?
- Convert the number to a string to allow for string manipulation.
- Initialize a for loop to run for the desired number of rotations.
- In each iteration, shift the first character of the array to the end.
- Join the rotated array back to a string.
- Convert the string back to a number and store it in an array to collect all rotations.
Rotate by moving the first digit to the end using the slice() method ?
numStr = numStr.slice(1) + numStr[0];
Convert back to a number using parseInt() ?
rotations.push(parseInt(numStr));
Example
Below is an example of generating all rotations of a given number using string manipulation ?
function generateRotations(num) { let numStr = num.toString(); let rotations = []; for (let i = 0; i < numStr.length; i++) { numStr = numStr.slice(1) + numStr[0]; rotations.push(parseInt(numStr)); } return rotations; } console.log(generateRotations(123));
Output
[231, 312, 123]
Time Complexity: O(n²) for a number with n digits, since slicing and concatenation happen in each iteration.
Space Complexity: O(n), the rotations array stores n numbers. Only a few integer variables used O(1)
Using Modular Arithmetic
Instead of using string operations, we can manipulate the number using mathematical operations such as division and modulus.
Following are the steps to generate all rotations of a number using modular arithmetic ?
- Determine the number of digits (count).
- Compute a divisor to extract the first digit.
- Loop through count times: Extract the first digit with the help of integer division. Extract the remaining digits with the help of modulus.
- Rotate the number by shifting the remaining digits left and putting the first digit in the last position.
- Store the new rotation in the rotations array.
Finding the divisor for extracting the first digit using the Math.pow() function ?
let divisor = Math.pow(10, count - 1);
Storing all the numbers in a rotation array using the push() method ?
rotations.push(num);
Example
Below is an example of generating all rotations of a given number using modular arithmetic ?
function generateRotationsMath(num) { let rotations = []; let count = num.toString().length; let divisor = Math.pow(10, count - 1); for (let i = 0; i < count; i++) { let firstDigit = Math.floor(num / divisor); let remainingDigits = num % divisor; num = remainingDigits * 10 + firstDigit; rotations.push(num); } return rotations; } console.log(generateRotationsMath(123)); // Output: [231, 312, 123]
Output
[231, 312, 123]
Time Complexity: O(n) since we perform constant-time arithmetic operations in each iteration.
Space Complexity: O(n), the rotations array stores n numbers. Only a few integer variables (count, divisor, etc.) are used O(1).
Conclusion
Both methods accomplish the objective of rotations of a certain number. However, the second method using modular arithmetic is more efficient: string manipulation offers a more straightforward coding, but becomes inefficient when large numbers are involved.