Javascript Program to Rotate digits of a given number by K Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 output is 34512 Input: N = 12345, K = -3Output: 34512 Explanation: Right rotating N(= 12345) by K( = -3) modifies N to 34512. Therefore, the required output is 34512 Approach: Follow the steps below to solve the problem: Initialize a variable, say X, to store the count of digits in N.Update K = (K + X) % X to reduce it to a case of left rotation.Remove the first K digits of N and append all the removed digits to the right of the digits of N.Finally, print the value of N. Below is the implementation of the above approach: JavaScript <script> // Javascript program to implement // the above approach // Function to find the count of // digits in N function numberOfDigit(N) { // Stores count of // digits in N let digit = 0; // Calculate the count // of digits in N while (N > 0) { // Update digit digit++; // Update N N = Math.floor( N / 10); } return digit; } // Function to rotate the digits of N by K function rotateNumberByK(N, K) { // Stores count of digits in N let X = numberOfDigit(N); // Update K so that only need to // handle left rotation K = ((K % X) + X) % X; // Stores first K digits of N let left_no = Math.floor (N / Math.floor(Math.pow(10, X - K))); // Remove first K digits of N N = N % Math.floor(Math.pow(10, X - K)); // Stores count of digits in left_no let left_digit = numberOfDigit(left_no); // Append left_no to the right of // digits of N N = (N * Math.floor(Math.pow(10, left_digit))) + left_no; document.write(N); } // Driver Code let N = 12345, K = 7; // Function Call rotateNumberByK(N, K); // This code is contributed by souravghosh0416. </script> Output: 34512 Time Complexity: O(log10N)Auxiliary Space: O(1) Please refer complete article on Rotate digits of a given number by K for more details! Comment More infoAdvertise with us Next Article Javascript Program to Rotate digits of a given number by K kartik Follow Improve Article Tags : Mathematical JavaScript Web Technologies DSA number-digits rotation +2 More Practice Tags : Mathematical Similar Reads Javascript Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati 2 min read 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 10 min read Javascript Program to 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 2 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 Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N. Examples: Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765. Input: N = 7092Output: 9270Explanati 9 min read Javascript Program to Print array after it is right rotated K times Given an Array of size N and a values K, around which we need to right rotate the array. How to quickly print the right rotated array?Examples :Â Â Input: Array[] = {1, 3, 5, 7, 9}, K = 2. Output: 7 9 1 3 5 Explanation: After 1st rotation - {9, 1, 3, 5, 7} After 2nd rotation - {7, 9, 1, 3, 5} Input: 2 min read Javascript Program to Inplace rotate square matrix by 90 degrees | Set 1 Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 5 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