Maximum value possible by rotating digits of a given number

• Difficulty Level : Expert
• Last Updated : 14 Aug, 2021

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 = 657
Output: 765
Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765.

Input: N = 7092
Output: 9270
Explanation:
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 N
• After completing the above steps, print the value of ans as the required answer.

Below is the implementation of the above approach:

C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum value``// possible by rotations of digits of N``void` `findLargestRotation(``int` `num)``{``    ``// Store the required result``    ``int` `ans = num;` `    ``// Store the number of digits``    ``int` `len = ``floor``(``log10``(num) + 1);` `    ``int` `x = ``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``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``int` `N = 657;``    ``findLargestRotation(N);` `    ``return` `0;``}`

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.`

Python3

 `# Python program for the above approach` `# Function to find the maximum value``# possible by rotations of digits of N``def` `findLargestRotation(num):``  ` `    ``# Store the required result``    ``ans ``=` `num``    ` `    ``# Store the number of digits``    ``length ``=` `len``(``str``(num))``    ``x ``=` `10``*``*``(length ``-` `1``)``    ` `    ``# Iterate over the range[1, len-1]``    ``for` `i ``in` `range``(``1``, length):``      ` `        ``# Store the unit's digit``        ``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``    ``print``(ans)` `# Driver Code``N ``=` `657``findLargestRotation(N)` `# This code is contributed by rohitsingh07052.`

C#

 `// C# program for the above approach``using` `System;``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``    ``double` `lg = (``double``)(Math.Log10(num) + 1);``    ``int` `len = (``int``)(Math.Floor(lg));``    ``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``    ``Console.Write(ans);``}` `// Driver Code``public` `static` `void` `Main(``string``[] args)``{``    ``int` `N = 657;``    ``findLargestRotation(N);``}``}` `// This code is contributed by souravghosh0416,`

Javascript

 ``

Output:

`765`

Time Complexity: O(log10N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up