Check whether all the rotations of a given number is greater than or equal to the given number or not
Last Updated :
19 Oct, 2023
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
Simple Approach:
Approach:
- Read the input number x.
- Compute the number of digits in x using log10(x) + 1, and store the result in n.
- For each value of k from 1 to n-1, do the following:
- Compute the k-cyclic shift of x using (x % (int)pow(10, k)) * (int)pow(10, n-k) + x / (int)pow(10, k), and store the result in k_shift.
- If k_shift is smaller than x, return false.
- If all k-cyclic shifts are greater than or equal to x, return true.
- Output “Yes” if the function returns true, and “No” otherwise.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool check_all_k_shifts( int x) {
int n = log10 (x) + 1;
for ( int k = 1; k < n; k++) {
int k_shift = (x % ( int ) pow (10, k)) * ( int ) pow (10, n-k) + x / ( int ) pow (10, k);
if (k_shift < x) {
return false ;
}
}
return true ;
}
int main()
{
int x=2214;
if (check_all_k_shifts(x)) {
cout << "Yes\n" ;
} else {
cout << "No\n" ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean checkAllKShifts( int x) {
int n = ( int ) (Math.log10(x) + 1 );
for ( int k = 1 ; k < n; k++) {
int kShift = (x % ( int ) Math.pow( 10 , k)) * ( int ) Math.pow( 10 , n - k) + x / ( int ) Math.pow( 10 , k);
if (kShift < x) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int x = 2214 ;
if (checkAllKShifts(x)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python
import math
def check_all_k_shifts(x):
n = int (math.log10(x)) + 1
for k in range ( 1 , n):
k_shift = (x % 10 * * k) * 10 * * (n - k) + \
x / / 10 * * k
if k_shift < x:
return False
return True
def main():
x = 2214
if check_all_k_shifts(x):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG
{
static bool CheckAllKShifts( int x)
{
int n = ( int )Math.Log10(x) + 1;
for ( int k = 1; k < n; k++)
{
int kShift = (x % ( int )Math.Pow(10, k)) * ( int )Math.Pow(10, n - k) + x / ( int )Math.Pow(10, k);
if (kShift < x)
{
return false ;
}
}
return true ;
}
static void Main( string [] args)
{
int x = 2214;
if (CheckAllKShifts(x))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function checkAllKShifts(x) {
let n = Math.floor(Math.log10(x)) + 1;
for (let k = 1; k < n; k++) {
let kShift = (x % Math.pow(10, k)) * Math.pow(10, n - k) + Math.floor(x / Math.pow(10, k));
if (kShift < x) {
return false ;
}
}
return true ;
}
let x = 2214;
if (checkAllKShifts(x)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(n^2), where n represents the length of the given string.
Space complexity: O(1).
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++
#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++)
{
x = (s.substr(i) + s.substr(0, i)).length();
if (x >= s.length())
{
continue ;
}
ff = false ;
break ;
}
if (ff)
{
cout << ( "Yes" );
}
else
{
cout << ( "No" );
}
}
int main()
{
int n = 3;
string s = "123" ;
CheckKCycles(n, s);
return 0;
}
|
Java
class GFG
{
static void CheckKCycles( int n, String s)
{
boolean ff = true ;
int x = 0 ;
for ( int i = 1 ; i < n; i++)
{
x = (s.substring(i) + s.substring( 0 , i)).length();
if (x >= s.length())
{
continue ;
}
ff = false ;
break ;
}
if (ff)
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String[] args)
{
int n = 3 ;
String s = "123" ;
CheckKCycles(n, s);
}
}
|
Python
def CheckKCycles(n, s):
ff = True
for i in range ( 1 , n):
x = int (s[i:] + s[ 0 :i])
if (x > = int (s)):
continue
ff = False
break
if (ff):
print ( "Yes" )
else :
print ( "No" )
n = 3
s = "123"
CheckKCycles(n, s)
|
C#
using System;
class GFG
{
static void CheckKCycles( int n, String s)
{
bool ff = true ;
int x = 0;
for ( int i = 1; i < n; i++)
{
x = (s.Substring(i) + s.Substring(0, i)).Length;
if (x >= s.Length)
{
continue ;
}
ff = false ;
break ;
}
if (ff)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static void Main(String[] args)
{
int n = 3;
String s = "123" ;
CheckKCycles(n, s);
}
}
|
Javascript
<script>
function CheckKCycles(n, s)
{
var ff = true ;
var x = 0;
for (i = 1; i < n; i++)
{
x = (s.substring(i) + s.substring(0, i)).length;
if (x >= s.length)
{
continue ;
}
ff = false ;
break ;
}
if (ff)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
}
var n = 3;
var s = "123" ;
CheckKCycles(n, s);
</script>
|
PHP
<?php
function CheckKCycles( $n , $s )
{
$ff = true;
$x = 0;
for ( $i = 1; $i < $n ; $i ++)
{
$x = strlen ( substr ( $s , $i ). substr ( $s , 0, $i ));
if ( $x >= strlen ( $s ))
{
continue ;
}
$ff = false;
break ;
}
if ( $ff )
{
print ( "Yes" );
}
else
{
print ( "No" );
}
}
$n = 3;
$s = "123" ;
CheckKCycles( $n , $s );
?>
|
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 code of this approach:
C++
#include <iostream>
#include <string>
using namespace std;
string hasKCycles( int n, string s) {
for ( int k = 1; k <= n/2; k++) {
if (n % k != 0) {
continue ;
}
int len = n/k;
bool flag = true ;
for ( int i = 1; i < k; i++) {
if (s.substr((i-1)*len, len) != s.substr(i*len, len)) {
flag = false ;
break ;
}
}
if (flag) {
return "Yes" ;
}
}
return "No" ;
}
int main() {
int n = 3;
string s = "123" ;
cout << hasKCycles(n, s) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 3 ;
String s = "123" ;
System.out.println(hasKCycles(n, s));
}
public static String hasKCycles( int n, String s) {
for ( int k = 1 ; k <= n/ 2 ; k++) {
if (n % k != 0 ) {
continue ;
}
int len = n/k;
boolean flag = true ;
for ( int i = 1 ; i < k; i++) {
if (!s.substring((i- 1 )*len, i*len).equals(s.substring(i*len, (i+ 1 )*len))) {
flag = false ;
break ;
}
}
if (flag) {
return "Yes" ;
}
}
return "No" ;
}
}
|
Python3
def has_k_cycles(n, s):
for k in range ( 1 , n / / 2 + 1 ):
if n % k ! = 0 :
continue
len_ = n / / k
flag = True
for i in range ( 1 , k):
if s[(i - 1 ) * len_ : i * len_] ! = s[i * len_ : (i + 1 ) * len_]:
flag = False
break
if flag:
return "Yes"
return "No"
if __name__ = = "__main__" :
n = 3
s = "123"
result = has_k_cycles(n, s)
print (result)
|
C#
using System;
class Program
{
static string HasKCycles( int n, string s)
{
for ( int k = 1; k <= n / 2; k++)
{
if (n % k != 0)
{
continue ;
}
int len = n / k;
bool flag = true ;
for ( int i = 1; i < k; i++)
{
if (s.Substring((i - 1) * len, len) != s.Substring(i * len, len))
{
flag = false ;
break ;
}
}
if (flag)
{
return "Yes" ;
}
}
return "No" ;
}
static void Main()
{
int n = 3;
string s = "123" ;
Console.WriteLine(HasKCycles(n, s));
}
}
|
Javascript
function GFG(n, s) {
for (let k = 1; k <= n / 2; k++) {
if (n % k !== 0) {
continue ;
}
const len = n / k;
let flag = true ;
for (let i = 1; i < k; i++) {
if (s.slice((i - 1) * len, i * len) !== s.slice(i * len, (i + 1) * len)) {
flag = false ;
break ;
}
}
if (flag) {
return "Yes" ;
}
}
return "No" ;
}
function main() {
const n = 3;
const s = "123" ;
console.log(GFG(n, s));
}
main();
|
Time Complexity: O(N^2), 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), because we only need to store a few variables to keep track of the loop variables and the result.
Share your thoughts in the comments
Please Login to comment...