Java 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: Java // Java program for the above approach import java.util.*; class GFG { // Function to find the maximum value // possible by rotations of digits of N static void findLargestRotation(int num) { // Store the required result int ans = num; // Store the number of digits int len = (int)Math.floor(((int)Math.log10(num)) + 1); int x = (int)Math.pow(10, len - 1); // Iterate over the range[1, len-1] for (int i = 1; i < len; i++) { // Store the unit's digit int lastDigit = num % 10; // Store the remaining number num = 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 System.out.print(ans); } // Driver Code public static void main(String[] args) { int N = 657; findLargestRotation(N); } } // This code is contributed by sanjoy_62. 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! Create Quiz Comment K kartik Follow 0 Improve K kartik Follow 0 Improve Article Tags : Java Technical Scripter 2020 number-digits rotation Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like