Wednesday, July 3, 2024
HomeLanguagesPhpPhp Program to Check whether all the rotations of a given number...

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

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: 
 

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


Output: 

Yes

 

Time Complexity: O(N2), where N is the length of the given string. First it runs a loop for traversing the string and inside that substring function is used. So, the code will run in O(N2) time.

Auxiliary Space: O(1), no extra space 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!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments