Java Program to Find Maximum value possible by rotating digits of a given number Last Updated : 27 Jan, 2022 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! Comment More infoAdvertise with us Next Article Java Program to Find Maximum value possible by rotating digits of a given number kartik Follow Improve Article Tags : Java Mathematical Java Programs DSA Technical Scripter 2020 number-digits rotation +3 More Practice Tags : JavaMathematical Similar Reads Java 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 Java Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i]. Examples :  Input: arr[] = {1, 20, 2, 10} Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 I 4 min read Java Program to Rotate bits of a number 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. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th 3 min read Java Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; 6 min read Java Program for Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1. Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 6 min read Java 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 Extract Maximum Numeric Value From A Given Alphanumeric String in Java In this article, we will learn to extract the maximum numeric value from a given alphanumeric string using Java. We will use regular expressions to identify and isolate numeric sequences within the string, and then compare these values to determine the largest one. This process consists of parsing a 3 min read Java Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns Given a square matrix, mat[][] of dimensions N * N, the task is find the maximum sum of diagonal elements possible from the given matrix by rotating either all the rows or all the columns of the matrix by a positive integer. Examples: Input: mat[][] = { { 1, 1, 2 }, { 2, 1, 2 }, { 1, 2, 2 } }Output: 3 min read Java Program to Check if all array elements can be converted to pronic numbers by rotating digits Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times. Examples: Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to 3 min read Java Program for Longest subsequence of a number having same left and right rotation Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation. Examples: Input: S = "100210601" Output: 4 Explanation: The subsequence "0000" satisfies the necessary condition. The subsequence "1010" generates the string "0101" 4 min read Like