Javascript Program to Find Maximum value possible by rotating digits of a given number Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: 9270Explanation:All rotations of 7092 are {7092, 2709, 9270, 0927}. The maximum value among all these rotations is 9270. Approach: The idea is to find all rotations of the number N and print the maximum among all the numbers generated. Follow the steps below to solve the problem: Count the number of digits present in the number N, i.e. upper bound of log10N.Initialize a variable, say ans with the value of N, to store the resultant maximum number generated.Iterate over the range [1, log10(N) - 1] and perform the following steps:Update the value of N with its next rotation.Now, if the next rotation generated exceeds ans, then update ans with the rotated value of NAfter completing the above steps, print the value of ans as the required answer. Below is the implementation of the above approach: JavaScript <script> // JavaScript program for the above approach // Function to find the maximum value // possible by rotations of digits of N function findLargestRotation(num) { // Store the required result let ans = num; // Store the number of digits let len = Math.floor(Math.log10(num) + 1); let x = Math.pow(10, len - 1); // Iterate over the range[1, len-1] for (let i = 1; i < len; i++) { // Store the unit's digit let lastDigit = num % 10; // Store the remaining number num = parseInt(num / 10); // Find the next rotation num += (lastDigit * x); // If the current rotation is // greater than the overall // answer, then update answer if (num > ans) { ans = num; } } // Print the result document.write(ans); } // Driver Code let N = 657; findLargestRotation(N); // This code is contributed by souravmahato348. </script> Output: 765 Time Complexity: O(log10N)Auxiliary Space: O(1) Please refer complete article on Maximum value possible by rotating digits of a given number for more details! Comment More info K kartik Follow Improve Article Tags : JavaScript Technical Scripter 2020 number-digits rotation Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readJavaScript Arrays7 min readJavaScript Strings6 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readMethodsJavaScript Array Methods7 min readJavaScript String Methods9 min readJavaScript Object Methods2 min readJavaScript JSON4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like