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++ program for the above approach #include <bits/stdc++.h> 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 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. |
# 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# 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, |
<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> |
765
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Convert number to string, rotate the string and convert back to integer in python:
Approach:
Initialize a variable max_num with the input number n.
Convert the input number n to a string str_num.
Loop through the indices of the string str_num, starting from index 1 to the end of the string.
Inside the loop, slice the string str_num from index 1 to the end and concatenate it with the first character of the string str_num using the string concatenation operator +.
Convert the resulting rotated string to an integer rotated_num.
Check if rotated_num is greater than max_num, if yes, update max_num to rotated_num.
Return max_num.
Test the function with some inputs and measure the execution time using the time module.
#include <iostream> #include <string> #include <ctime> using namespace std; // Import the standard namespace
// Function to find the maximum rotation of an integer int maxRotationApproach1( int n) {
int maxNum = n;
string strNum = to_string(n);
// Iterate through all possible rotations
for ( int i = 1; i < strNum.length(); ++i) {
// Rotate the string to the left
strNum = strNum.substr(1) + strNum[0];
int rotatedNum = stoi(strNum);
if (rotatedNum > maxNum) {
maxNum = rotatedNum;
}
}
return maxNum;
} int main() {
// Test the function with input N = 657 and N = 7092
clock_t start = clock ();
int result1 = maxRotationApproach1(657);
int result2 = maxRotationApproach1(7092);
// Print the results
cout << "Max Rotation for 657: " << result1 << endl; // Output: 765
cout << "Max Rotation for 7092: " << result2 << endl; // Output: 9270
return 0;
} |
import java.util.Arrays;
public class Main {
// Function to find the maximum rotation of an integer
static int maxRotationApproach1( int n) {
int maxNum = n;
String strNum = Integer.toString(n);
// Iterate through all possible rotations
for ( int i = 1 ; i < strNum.length(); ++i) {
// Rotate the string to the left
strNum = strNum.substring( 1 ) + strNum.charAt( 0 );
int rotatedNum = Integer.parseInt(strNum);
if (rotatedNum > maxNum) {
maxNum = rotatedNum;
}
}
return maxNum;
}
public static void main(String[] args) {
// Test the function with input N = 657 and N = 7092
long startTime = System.nanoTime();
int result1 = maxRotationApproach1( 657 );
int result2 = maxRotationApproach1( 7092 );
long endTime = System.nanoTime();
// Print the results
System.out.println(result1); // Output: 765
System.out.println(result2); // Output: 9270
}
} |
import time
def max_rotation_approach1(n):
max_num = n
str_num = str (n)
for i in range ( 1 , len (str_num)):
str_num = str_num[ 1 :] + str_num[ 0 ]
rotated_num = int (str_num)
if rotated_num > max_num:
max_num = rotated_num
return max_num
# Test the function with input N = 657 and N = 7092 start = time.time()
print (max_rotation_approach1( 657 )) # Output: 765
print (max_rotation_approach1( 7092 )) # Output: 9270
end = time.time()
|
using System;
class Program
{ // Function to find the maximum rotation of an integer
static int MaxRotationApproach1( int n)
{
int maxNum = n;
string strNum = n.ToString();
// Iterate through all possible rotations
for ( int i = 1; i < strNum.Length; ++i)
{
// Rotate the string to the left
strNum = strNum.Substring(1) + strNum[0];
int rotatedNum = int .Parse(strNum);
if (rotatedNum > maxNum)
{
maxNum = rotatedNum;
}
}
return maxNum;
}
static void Main()
{
// Test the function with input N = 657 and N = 7092
DateTime start = DateTime.Now;
int result1 = MaxRotationApproach1(657);
int result2 = MaxRotationApproach1(7092);
// Print the results
Console.WriteLine( "Max Rotation for 657: " + result1); // Output: 765
Console.WriteLine( "Max Rotation for 7092: " + result2); // Output: 9270
TimeSpan elapsed = DateTime.Now - start;
Console.WriteLine( "Execution time: " + elapsed.TotalMilliseconds + " ms" );
}
} |
// Function to find the maximum rotation of an integer function maxRotationApproach1(n) {
let maxNum = n;
let strNum = n.toString();
// Iterate through all possible rotations
for (let i = 1; i < strNum.length; i++) {
// Rotate the string to the left
strNum = strNum.substring(1) + strNum[0];
let rotatedNum = parseInt(strNum, 10);
if (rotatedNum > maxNum) {
maxNum = rotatedNum;
}
}
return maxNum;
} // Main function function main() {
// Test the function with input N = 657 and N = 7092
let start = new Date().getTime();
let result1 = maxRotationApproach1(657);
let result2 = maxRotationApproach1(7092);
// Print the results
console.log( "Max Rotation for 657: " + result1); // Output: 765
console.log( "Max Rotation for 7092: " + result2); // Output: 9270
let end = new Date().getTime();
let timeTaken = end - start;
console.log( "Time taken: " + timeTaken + " milliseconds" );
} main(); |
Max Rotation for 657: 765 Max Rotation for 7092: 9270
Time complexity: O(n^2), where n is the number of digits in the input number.
Auxiliary Space: O(n), where n is the number of digits in the input number.