Maximum value possible by rotating digits of a given number
Last Updated :
10 Jan, 2024
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++
#include <bits/stdc++.h>
using namespace std;
void findLargestRotation( int num)
{
int ans = num;
int len = floor ( log10 (num) + 1);
int x = pow (10, len - 1);
for ( int i = 1; i < len; i++) {
int lastDigit = num % 10;
num = num / 10;
num += (lastDigit * x);
if (num > ans) {
ans = num;
}
}
cout << ans;
}
int main()
{
int N = 657;
findLargestRotation(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findLargestRotation( int num)
{
int ans = num;
int len = ( int )Math.floor((( int )Math.log10(num)) + 1 );
int x = ( int )Math.pow( 10 , len - 1 );
for ( int i = 1 ; i < len; i++) {
int lastDigit = num % 10 ;
num = num / 10 ;
num += (lastDigit * x);
if (num > ans) {
ans = num;
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int N = 657 ;
findLargestRotation(N);
}
}
|
Python3
def findLargestRotation(num):
ans = num
length = len ( str (num))
x = 10 * * (length - 1 )
for i in range ( 1 , length):
lastDigit = num % 10
num = num / / 10
num + = (lastDigit * x)
if (num > ans):
ans = num
print (ans)
N = 657
findLargestRotation(N)
|
C#
using System;
class GFG{
static void findLargestRotation( int num)
{
int ans = num;
double lg = ( double )(Math.Log10(num) + 1);
int len = ( int )(Math.Floor(lg));
int x = ( int )Math.Pow(10, len - 1);
for ( int i = 1; i < len; i++) {
int lastDigit = num % 10;
num = num / 10;
num += (lastDigit * x);
if (num > ans) {
ans = num;
}
}
Console.Write(ans);
}
public static void Main( string [] args)
{
int N = 657;
findLargestRotation(N);
}
}
|
Javascript
<script>
function findLargestRotation(num)
{
let ans = num;
let len = Math.floor(Math.log10(num) + 1);
let x = Math.pow(10, len - 1);
for (let i = 1; i < len; i++) {
let lastDigit = num % 10;
num = parseInt(num / 10);
num += (lastDigit * x);
if (num > ans) {
ans = num;
}
}
document.write(ans);
}
let N = 657;
findLargestRotation(N);
</script>
|
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.
C++
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int maxRotationApproach1( int n) {
int maxNum = n;
string strNum = to_string(n);
for ( int i = 1; i < strNum.length(); ++i) {
strNum = strNum.substr(1) + strNum[0];
int rotatedNum = stoi(strNum);
if (rotatedNum > maxNum) {
maxNum = rotatedNum;
}
}
return maxNum;
}
int main() {
clock_t start = clock ();
int result1 = maxRotationApproach1(657);
int result2 = maxRotationApproach1(7092);
cout << "Max Rotation for 657: " << result1 << endl;
cout << "Max Rotation for 7092: " << result2 << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static int maxRotationApproach1( int n) {
int maxNum = n;
String strNum = Integer.toString(n);
for ( int i = 1 ; i < strNum.length(); ++i) {
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) {
long startTime = System.nanoTime();
int result1 = maxRotationApproach1( 657 );
int result2 = maxRotationApproach1( 7092 );
long endTime = System.nanoTime();
System.out.println(result1);
System.out.println(result2);
}
}
|
Python3
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
start = time.time()
print (max_rotation_approach1( 657 ))
print (max_rotation_approach1( 7092 ))
end = time.time()
|
C#
using System;
class Program
{
static int MaxRotationApproach1( int n)
{
int maxNum = n;
string strNum = n.ToString();
for ( int i = 1; i < strNum.Length; ++i)
{
strNum = strNum.Substring(1) + strNum[0];
int rotatedNum = int .Parse(strNum);
if (rotatedNum > maxNum)
{
maxNum = rotatedNum;
}
}
return maxNum;
}
static void Main()
{
DateTime start = DateTime.Now;
int result1 = MaxRotationApproach1(657);
int result2 = MaxRotationApproach1(7092);
Console.WriteLine( "Max Rotation for 657: " + result1);
Console.WriteLine( "Max Rotation for 7092: " + result2);
TimeSpan elapsed = DateTime.Now - start;
Console.WriteLine( "Execution time: " + elapsed.TotalMilliseconds + " ms" );
}
}
|
Javascript
function maxRotationApproach1(n) {
let maxNum = n;
let strNum = n.toString();
for (let i = 1; i < strNum.length; i++) {
strNum = strNum.substring(1) + strNum[0];
let rotatedNum = parseInt(strNum, 10);
if (rotatedNum > maxNum) {
maxNum = rotatedNum;
}
}
return maxNum;
}
function main() {
let start = new Date().getTime();
let result1 = maxRotationApproach1(657);
let result2 = maxRotationApproach1(7092);
console.log( "Max Rotation for 657: " + result1);
console.log( "Max Rotation for 7092: " + result2);
let end = new Date().getTime();
let timeTaken = end - start;
console.log( "Time taken: " + timeTaken + " milliseconds" );
}
main();
|
Output
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.
Share your thoughts in the comments
Please Login to comment...