Open In App

Python3 Program to Check whether all the rotations of a given number is greater than or equal to the given number or not

Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. 
A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. 
For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the given condition is satisfied else print No.
Examples: 
 

Input: x = 123 
Output : Yes 
The k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. 
Both 312 and 231 are greater than 123.
Input: 2214 
Output: No 
The k-cyclic shift of 2214 when k=2 is 1422 which is smaller than 2214 
 

 

Approach: Simply find all the possible k cyclic shifts of the number and check if all are greater than the given number or not.
Below is the implementation of the above approach: 
 

Python




# Python3 implementation of the approach
def CheckKCycles(n, s):
    ff = True
    for i in range(1, n):
 
        # Splitting the number at index i
        # and adding to the front
        x = int(s[i:] + s[0:i])
 
        # Checking if the value is greater than
        # or equal to the given value
        if (x >= int(s)):
            continue
        ff = False
        break
    if (ff):
        print("Yes")
    else:
        print("No")
 
n = 3
s = "123"
CheckKCycles(n, s)


Output: 

Yes

 

Time Complexity: O(N2), where N represents the length of the given string.

The time complexity of the program is O(N2) because first it runs a loop for traversing the string and inside that substring function is used.

Auxiliary Space: O(1), no extra space is required, so it is a constant.

Approach 2:

  • Here’s another approach to solve the same problem:
  • Iterate through all possible k values from 1 to n/2.
  • For each k value, check if the string can be split into k cycles of length n/k. To do this, compare the substring of the original string from 0 to n/k with the substring of the original string from i*(n/k) to (i+1)*(n/k), for all i from 1 to k-1.
  • If the string can be split into k cycles of length n/k, then it satisfies the given condition. Return “Yes”.
  • If the string cannot be split into any cycles, then return “No”.
  • Here’s the Python implementation of this approach:

Python3




def has_k_cycles(n, s):
    for k in range(1, n//2 + 1):
        if n % k != 0:
            continue
 
        length = n // k
        flag = True
 
        for i in range(1, k):
            if s[(i-1)*length:i*length] != s[i*length:(i+1)*length]:
                flag = False
                break
 
        if flag:
            return "Yes"
 
    return "No"
 
n = 3
s = "123"
print(has_k_cycles(n, s))


Output:

YES

Time Complexity: O(N^2), where N represents the length of the given string.

Auxiliary Space: O(1), no extra space is required, so it is a constant.

Please refer complete article on Check whether all the rotations of a given number is greater than or equal to the given number or not for more details!



Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads