Open In App

Python3 Program to Rotate digits of a given number by K

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

INTRODUCTION:

One important point to consider when working with the algorithm to rotate the digits of a given number by k positions is the time complexity.

If we were to implement this algorithm using the approach shown in the previous example, the time complexity would be O(n), where n is the number of digits in the input number. This is because the algorithm must iterate through each digit of the number to perform the rotation.

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: 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:

Python3




# Python3 program to implement
# the above approach
 
# Function to find the count of
# digits in N
def numberOfDigit(N):
 
    # Stores count of
    # digits in N
    digit = 0
 
    # Calculate the count
    # of digits in N
    while (N > 0):
 
        # Update digit
        digit += 1
 
        # Update N
        N //= 10
    return digit
 
# Function to rotate the digits of N by K
def rotateNumberByK(N, K):
 
    # Stores count of digits in N
    X = numberOfDigit(N)
 
    # Update K so that only need to
    # handle left rotation
    K = ((K % X) + X) % X
 
    # Stores first K digits of N
    left_no = N // pow(10, X - K)
 
    # Remove first K digits of N
    N = N % pow(10, X - K)
 
    # Stores count of digits in left_no
    left_digit = numberOfDigit(left_no)
 
    # Append left_no to the right of
    # digits of N
    N = N * pow(10, left_digit) + left_no
    print(N)
 
# Driver Code
if __name__ == '__main__':
    N, K = 12345, 7
 
    # Function Call
    rotateNumberByK(N, K)
 
    # This code is contributed by mohit kumar 29


Output: 

34512

 

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

EXAMPLE 2:

Python3




def rotate_digits(num, k):
  # convert number to string and store in a list
  num_list =
 
  # rotate the list by k
  num_list = num_list[k:] + num_list[:k]
 
  # convert the list back to a number and return it
  return int(''.join(num_list))
 
# test the function
print(rotate_digits(12345, 2))  # output: 34512
print(rotate_digits(12345, 4))  # output: 23451
print(rotate_digits(12345, 6))  # output: 12345


Output

34512
51234
12345

This code defines a function rotate_digits that takes an integer num and an integer k as input. It first converts the number to a string and stores the individual digits in a list. It then rotates the list by k positions using slicing, and converts the rotated list back to a number using join. The resulting rotated number is returned by the function.

The function is then called with several test cases to demonstrate its behavior.

This implementation uses modulo and integer division operations to separate the digits of the number into two parts, and then combines them in the appropriate order to form the rotated number. Since these operations are O(1), the overall time complexity of the algorithm is also O(1).

Please refer complete article on Rotate digits of a given number by K for more details!



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads