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 = 2
Output: 34512
Explanation:
Left rotating N(= 12345) by K(= 2) modifies N to 34512.
Therefore, the required output is 34512
Input: N = 12345, K = -3
Output: 34512
Explanation:
Right rotating N(= 12345) by K( = -3) modifies N to 34512.
Therefore, the required output is 34512
Approach:
Approach to solve this problem is to convert the given number N into a string, and then perform string rotations using substrings and concatenation operations. For left rotation, we can extract the first K digits of the string, and append them to the end of the remaining digits of the string. For right rotation, we can extract the last K digits of the string, and prepend them to the beginning of the remaining digits of the string. Finally, we can convert the resulting rotated string back to an integer and return it as the output.
Here are the steps of approach:
- Convert the given integer N to a string.
- Check whether K is positive or negative. If K is positive, perform a left rotation; otherwise, perform a right rotation.
- For left rotation, extract the first K digits of the string using the substr() function and store it in a new string. Then, extract the remaining digits of the string using substr() and append the first K digits at the end of it using concatenation.
- For right rotation, extract the last K digits of the string using substr() and store it in a new string. Then, extract the remaining digits of the string using substr() and prepend the last K digits at the beginning of it using concatenation.
- Convert the rotated string back to an integer using stoi() function.
- Return the rotated integer as the output.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rotateNumberByK( int N, int K)
{
string num = to_string(N);
int len = num.length();
if (K > 0){
string rotated = num.substr(K) + num.substr(0, K);
cout << stoi(rotated);
} else {
K = abs (K);
string rotated = num.substr(len-K) + num.substr(0, len-K);
cout << stoi(rotated);
}
}
int main()
{
int N = 12345, K = 2;
rotateNumberByK(N, K);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void rotateNumberByK( int N, int K)
{
String num = Integer.toString(N);
int len = num.length();
if (K > 0 ) {
String rotated
= num.substring(K) + num.substring( 0 , K);
System.out.print(Integer.parseInt(rotated));
}
else {
K = Math.abs(K);
String rotated = num.substring(len - K)
+ num.substring( 0 , len - K);
System.out.print(Integer.parseInt(rotated));
}
}
public static void main(String[] args)
{
int N = 12345 , K = 2 ;
rotateNumberByK(N, K);
}
}
|
Python3
def rotate_number_by_k(N, K):
num = str (N)
length = len (num)
if K > 0 :
rotated = num[K:] + num[:K]
print ( int (rotated))
else :
K = abs (K)
rotated = num[length - K:] + num[:length - K]
print ( int (rotated))
if __name__ = = "__main__" :
N = 12345
K = 2
rotate_number_by_k(N, K)
|
C#
using System;
public class GFG {
public static void RotateNumberByK( int N, int K)
{
string num = N.ToString();
int len = num.Length;
if (K > 0) {
string rotated
= num.Substring(K) + num.Substring(0, K);
Console.WriteLine( int .Parse(rotated));
}
else {
K = Math.Abs(K);
string rotated = num.Substring(len - K)
+ num.Substring(0, len - K);
Console.WriteLine( int .Parse(rotated));
}
}
public static void Main()
{
int N = 12345, K = 2;
RotateNumberByK(N, K);
}
}
|
Javascript
function rotateNumberByK(N, K) {
let num = N.toString();
let len = num.length;
if (K > 0) {
let rotated = num.substring(K) + num.substring(0, K);
console.log(parseInt(rotated));
} else {
K = Math.abs(K);
let rotated = num.substring(len - K) + num.substring(0, len - K);
console.log(parseInt(rotated));
}
}
const N = 12345;
const K = 2;
rotateNumberByK(N, K);
|
Time Complexity: O(N), where N is the number of digits in the given integer.
Space Complexity: O(N), as it involves creating a string representation of the integer and storing it in memory.
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say X, to store the count of digits in N.
- Update K = (K + X) % X to reduce it to a case of left rotation.
- Remove the first K digits of N and append all the removed digits to the right of the digits of N.
- Finally, print the value of N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfDigit( int N)
{
int digit = 0;
while (N > 0) {
digit++;
N /= 10;
}
return digit;
}
void rotateNumberByK( int N, int K)
{
int X = numberOfDigit(N);
K = ((K % X) + X) % X;
int left_no = N / ( int )( pow (10, X - K));
N = N % ( int )( pow (10, X - K));
int left_digit = numberOfDigit(left_no);
N = (N * ( int )( pow (10, left_digit))) + left_no;
cout << N;
}
int main()
{
int N = 12345, K = 7;
rotateNumberByK(N, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfDigit( int N)
{
int digit = 0 ;
while (N > 0 ) {
digit++;
N /= 10 ;
}
return digit;
}
static void rotateNumberByK( int N, int K)
{
int X = numberOfDigit(N);
K = ((K % X) + X) % X;
int left_no = N / ( int )(Math.pow( 10 ,
X - K));
N = N % ( int )(Math.pow( 10 , X - K));
int left_digit = numberOfDigit(left_no);
N = (N * ( int )(Math.pow( 10 , left_digit)))
+ left_no;
System.out.println(N);
}
public static void main(String args[])
{
int N = 12345 , K = 7 ;
rotateNumberByK(N, K);
}
}
|
Python3
def numberOfDigit(N):
digit = 0
while (N > 0 ):
digit + = 1
N / / = 10
return digit
def rotateNumberByK(N, K):
X = numberOfDigit(N)
K = ((K % X) + X) % X
left_no = N / / pow ( 10 , X - K)
N = N % pow ( 10 , X - K)
left_digit = numberOfDigit(left_no)
N = N * pow ( 10 , left_digit) + left_no
print (N)
if __name__ = = '__main__' :
N, K = 12345 , 7
rotateNumberByK(N, K)
|
C#
using System;
class GFG
{
static int numberOfDigit( int N)
{
int digit = 0;
while (N > 0)
{
digit++;
N /= 10;
}
return digit;
}
static void rotateNumberByK( int N, int K)
{
int X = numberOfDigit(N);
K = ((K % X) + X) % X;
int left_no = N / ( int )(Math.Pow(10,
X - K));
N = N % ( int )(Math.Pow(10, X - K));
int left_digit = numberOfDigit(left_no);
N = (N * ( int )(Math.Pow(10, left_digit)))
+ left_no;
Console.WriteLine(N);
}
public static void Main( string []args)
{
int N = 12345, K = 7;
rotateNumberByK(N, K);
}
}
|
Javascript
<script>
function numberOfDigit(N)
{
let digit = 0;
while (N > 0) {
digit++;
N = Math.floor( N / 10);
}
return digit;
}
function rotateNumberByK(N, K)
{
let X = numberOfDigit(N);
K = ((K % X) + X) % X;
let left_no = Math.floor (N / Math.floor(Math.pow(10,
X - K)));
N = N % Math.floor(Math.pow(10, X - K));
let left_digit = numberOfDigit(left_no);
N = (N * Math.floor(Math.pow(10, left_digit)))
+ left_no;
document.write(N);
}
let N = 12345, K = 7;
rotateNumberByK(N, K);
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Last Updated :
20 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...