Javascript Program to Generate all rotations of a number Last Updated : 13 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 312Input: n = 1445 Output: 4451 4514 5144 Approach: Assume n = 123.Multiply n with 10 i.e. n = n * 10 = 1230.Add the first digit to the resultant number i.e. 1230 + 1 = 1231.Subtract (first digit) * 10k from the resultant number where k is the number of digits in the original number (in this case, k = 3).1231 - 1000 = 231 is the left shift number of the original number. Below is the implementation of the above approach: JavaScript <script> // Javascript implementation of the approach // Function to return the count of digits of n function numberOfDigits(n) { let cnt = 0; while (n > 0) { cnt++; n = parseInt(n / 10, 10); } return cnt; } // Function to print the left shift numbers function cal(num) { let digits = numberOfDigits(num); let powTen = Math.pow(10, digits - 1); for (let i = 0; i < digits - 1; i++) { let firstDigit = parseInt(num / powTen, 10); // Formula to calculate left shift // from previous number let left = ((num * 10) + firstDigit) - (firstDigit * powTen * 10); document.write(left + " "); // Update the original number num = left; } } let num = 1445; cal(num); </script> Output: 4451 4514 5144 Time Complexity: O(log10(num))Auxiliary Space: O(1) Approach: Assume n = 123 let temp = str(n) + str(n) i.e 123123Now iterate through the temp and take slice 3 digits from iterating index of temp which is 231.Iterate through temp till the iterator is less than the half of length of temp. Below is the implementation of above approach: JavaScript <script> // Function to print the left shift numbers function cal(num) { var temp = '' + num var len = temp.length temp += temp; for( let i = 1 ; i*2 <temp.length ; i++){ console.log(temp.slice(i , i + len )) } } let num = 1445; cal(num); </script> Output: 4451 4514 5144 Time Complexity: O(n)Auxiliary Space: O(n) Please refer complete article on Generate all rotations of a number for more details! Comment More infoAdvertise with us Next Article Javascript Program to Generate all rotations of a number kartik Follow Improve Article Tags : Mathematical JavaScript Web Technologies DSA Akamai number-digits rotation +3 More Practice Tags : Mathematical Similar Reads Javascript Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required 2 min read Javascript Program to Find element at given index after a number of rotations An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find element at a given index.Examples : Input : arr[] : {1, 2, 3, 4, 5} ranges[] = { {0, 2}, {0, 3} } index : 1 Output : 3 Explanation 4 min read Generate all rotations of a number Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31 5 min read Javascript Program to Count of rotations required to generate a sorted array Given an array arr[], the task is to find the number of rotations required to convert the given array to sorted form.Examples: Input: arr[] = {4, 5, 1, 2, 3} Output: 2 Explanation: Sorted array {1, 2, 3, 4, 5} after 2 anti-clockwise rotations. Input: arr[] = {2, 1, 2, 2, 2} Output: 1 Explanation: So 4 min read Javascript Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y (0 < x, y < 2^32), check if one integer is obtained by rotating bits of the other. Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end.Examples: Input : a 3 min read Javascript Program to Rotate Matrix Elements Given a matrix, clockwise rotate elements in it.Examples:Input: 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12The idea is to use loops similar to the program for printing a matrix in spiral form. 5 min read Generate all cyclic permutations of a number 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_0 , a_1 , ..., a_n , a cyclic permutation of one place to the left would yield a_1 , ..., a_n , a_0 , and a 6 min read Javascript Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations are 2 min read Javascript Program to Count rotations divisible by 8 Given a large positive number as string, count all rotations of the given number which are divisible by 8.Examples: Input: 8Output: 1Input: 40Output: 1Rotation: 40 is divisible by 8 04 is not divisible by 8Input : 13502Output : 0No rotation is divisible by 8Input : 43262488612Output : 4Approach: It 3 min read Javascript Program to Find Mth element after K Right Rotations of an Array Given non-negative integers K, M, and an array arr[ ] consisting of N elements, the task is to find the Mth element of the array after K right rotations. Examples: Input: arr[] = {3, 4, 5, 23}, K = 2, M = 1 Output: 5 Explanation: The array after first right rotation a1[ ] = {23, 3, 4, 5} The array a 8 min read Like