Skip to content
Related Articles

Related Articles

Check whether all the rotations of a given number is greater than or equal to the given number or not
  • Difficulty Level : Basic
  • Last Updated : 14 Apr, 2021

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: 
 

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




// Java implementation of the approach
class GFG
{
 
    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
            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)
        {
            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 contributed by PrinciRaj1992 */

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)

C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    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
            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.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
 
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 3;
        String s = "123";
        CheckKCycles(n, s);
    }
}
 
// This code has been contributed by 29AjayKumar

PHP




<?php
// PHP implementation of the approach
 
function CheckKCycles($n, $s)
{
    $ff = true;
    $x = 0;
    for ($i = 1; $i < $n; $i++)
    {
 
        // Splitting the number at index i
        // and adding to the front
        $x = strlen(substr($s, $i).substr($s, 0, $i));
 
        // Checking if the value is greater than
        // or equal to the given value
        if ($x >= strlen($s))
        {
            continue;
        }
        $ff = false;
        break;
    }
    if ($ff)
    {
        print("Yes");
    }
    else
    {
        print("No");
    }
}
 
// Driver code
$n = 3;
$s = "123";
CheckKCycles($n, $s);
 
// This code contributed by mits
?>

Javascript




<script>
 
// javascript implementation of the approach
function CheckKCycles(n, s)
{
    var ff = true;
    var x = 0;
    for (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)
    {
        document.write("Yes");
    }
    else
    {
        document.write("No");
    }
}
 
// Driver code
    var n = 3;
    var s = "123";
    CheckKCycles(n, s);
 
// This code is contributed by 29AjayKumar
</script>
Output: 
Yes

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA




My Personal Notes arrow_drop_up
Recommended Articles
Page :