Open In App

C++ 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 1: 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: 
 

C++




//CPP implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
void CheckKCycles(int n, string s)
{
    bool ff = true;
    int x = 0;
    for (int i = 1; i < n; i++)
    {
 
        // Splitting the number at index i
        // and adding to the front
        x = (s.substr(i) + s.substr(0, i)).length();
 
        // Checking if the value is greater than
        // or equal to the given value
        if (x >= s.length())
        {
            continue;
        }
        ff = false;
        break;
    }
    if (ff)
    {
        cout << ("Yes");
    }
    else
    {
        cout << ("No");
    }
}
 
// Driver code
int main()
{
    int n = 3;
    string s = "123";
    CheckKCycles(n, s);
    return 0;
}
 
/* This code contributed by Rajput-Ji */


Java




public class Main {
     
    static void checkKCycles(int n, String s) {
        boolean ff = true;
        int x = 0;
        for (int i = 1; i < n; i++) {
 
            // Splitting the number at index i
            // and adding to the front
            String rotatedString = s.substring(i) + s.substring(0, i);
            x = rotatedString.length();
 
            // Checking if the value is greater than
            // or equal to the given value
            if (x >= s.length()) {
                continue;
            }
            ff = false;
            break;
        }
        if (ff) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
 
    // Driver code
    public static void main(String[] args) {
        int n = 3;
        String s = "123";
        checkKCycles(n, s);
    }
}
// this code is contibuted by utkarsh


Python




def check_k_cycles(n, s):
    ff = True
    x = 0
    for i in range(1, n):
        # Splitting the string at index i and adding to the front
        x = len(s[i:] + s[:i])
 
        # Checking if the value is greater than or equal to the length of the string
        if x >= len(s):
            continue
        ff = False
        break
 
    if ff:
        print("Yes")
    else:
        print("No")
 
# Driver code
n = 3
s = "123"
check_k_cycles(n, s)


C#




using System;
 
class Program
{
    static void CheckKCycles(int n, string s)
    {
        bool ff = true;
        int x = 0;
        for (int i = 1; i < n; i++)
        {
            // Splitting the number at index i
            // and adding to the front
            string rotatedString = s.Substring(i) + s.Substring(0, i);
            x = rotatedString.Length;
 
            // Checking if the value is greater than
            // or equal to the given value
            if (x >= s.Length)
            {
                continue;
            }
            ff = false;
            break;
        }
        if (ff)
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
 
    // Driver code
    static void Main(string[] args)
    {
        int n = 3;
        string s = "123";
        CheckKCycles(n, s);
    }
}
// this code is contibuted by utkarsh


Javascript




function checkKCycles(n, s) {
    let ff = true;
    let x = 0;
    for (let i = 1; i < n; i++) {
        // Splitting the number at index i and adding to the front
        x = (s.substring(i) + s.substring(0, i)).length;
 
        // Checking if the value is greater than or equal to the given value
        if (x >= s.length) {
            continue;
        }
        ff = false;
        break;
    }
    if (ff) {
        console.log("Yes");
    } else {
        console.log("No");
    }
}
 
// Driver code
let n = 3;
let 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: 

  1. Define a function CheckKCycles that takes an integer n and a string s as input.
  2. Initialize a boolean variable isKCycle as true.
  3. Iterate over the string starting from index 1 up to n-1.
  4. Compare characters s[i] and s[i % n].
  5. If the characters are not equal, set isKCycle as false and break the loop.
  6. After the loop, check the value of isKCycle.
  7. If it is true, print Yes to indicate a K-Cycle.
  8. If it is false, print No to indicate it is not a K-Cycle.

Below is the implementation of the above approach: 

C++




#include <iostream>
#include <string>
 
void CheckKCycles(int n, const std::string& s) {
    bool isKCycle = true;
    for (int i = 1; i < n; i++) {
        if (s[i] != s[i % n]) {
            isKCycle = false;
            break;
        }
    }
    if (isKCycle) {
        std::cout << "Yes";
    } else {
        std::cout << "No";
    }
}
// Nikunj Sonigara
int main() {
    int n = 3;
    std::string s = "123";
    CheckKCycles(n, s);
    return 0;
}


Java




public class Main {
    static void checkKCycles(int n, String s) {
        boolean isKCycle = true;
        for (int i = 1; i < n; i++) {
            if (s.charAt(i) != s.charAt(i % n)) {
                isKCycle = false;
                break;
            }
        }
        if (isKCycle) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
 
    public static void main(String[] args) {
        int n = 3;
        String s = "123";
        checkKCycles(n, s);
    }
}


Python3




def check_k_cycles(n, s):
    is_k_cycle = all(s[i] == s[i % n] for i in range(1, n))
    if is_k_cycle:
        print("Yes")
    else:
        print("No")
 
# Nikunj Sonigara
def main():
    n = 3
    s = "123"
    check_k_cycles(n, s)
 
if __name__ == "__main__":
    main()


Output

Yes





Time Complexity: O(N)

Auxiliary Space: O(1)

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 : 27 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads