# Check if an integer is rotation of another given integer

• Difficulty Level : Hard
• Last Updated : 13 Oct, 2022

Given two integers A and B, the task is to check if the integer A is rotation of the digits of the integer B or not. If found to be true, then print “Yes”. Otherwise print “No”.

Examples:

Input: A= 976, B= 679
Output: Yes

Input: A= 974, B= 2345
Output: No

Approach: Follow the steps below to solve the problem:

• If A == B, then print “Yes”.
• Calculate the count of digits of integer present in the integer A and B in variables, say dig1 and dig2.
• If dig1 != dig2 is found not be true, then print “No”.
• Initialize a variable, say temp. Assign temp = A.
• Now, iterate and perform the following operations:

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to check if the integer``// A is a rotation of the integer B``int` `check(``int` `A, ``int` `B)``{` `    ``if` `(A == B) {``        ``return` `1;``    ``}` `    ``// Stores the count of digits in A``    ``int` `dig1 = ``floor``(``log10``(A) + 1);` `    ``// Stores the count of digits in B``    ``int` `dig2 = ``floor``(``log10``(B) + 1);` `    ``// If dig1 not equal to dig2``    ``if` `(dig1 != dig2) {``        ``return` `0;``    ``}` `    ``int` `temp = A;` `    ``while` `(1) {` `        ``// Stores position of first digit``        ``int` `power = ``pow``(10, dig1 - 1);` `        ``// Stores the first digit``        ``int` `firstdigit = A / power;` `        ``// Rotate the digits of the integer``        ``A = A - firstdigit * power;``        ``A = A * 10 + firstdigit;` `        ``// If A is equal to B``        ``if` `(A == B) {``            ``return` `1;``        ``}``        ``// If A is equal to the initial``// value of integer A``        ``if` `(A == temp) {``            ``return` `0;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `A = 967, B = 679;` `    ``if` `(check(A, B))``        ``cout << ``"Yes"``;``    ``else``        ``cout << ``"No"` `<< endl;``    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.io.*;``class` `GFG {` `    ``// Function to check if the integer``    ``// A is a rotation of the integer B``    ``static` `int` `check(``int` `A, ``int` `B)``    ``{` `        ``if` `(A == B) {``            ``return` `1``;``        ``}` `        ``// Stores the count of digits in A``        ``int` `dig1 = (``int``)Math.floor(Math.log10(A) + ``1``);` `        ``// Stores the count of digits in B``        ``int` `dig2 = (``int``)Math.floor(Math.log10(B) + ``1``);` `        ``// If dig1 not equal to dig2``        ``if` `(dig1 != dig2) {``            ``return` `0``;``        ``}` `        ``int` `temp = A;` `        ``while` `(``true``) {` `            ``// Stores position of first digit``            ``int` `power = (``int``)Math.pow(``10``, dig1 - ``1``);` `            ``// Stores the first digit``            ``int` `firstdigit = A / power;` `            ``// Rotate the digits of the integer``            ``A = A - firstdigit * power;``            ``A = A * ``10` `+ firstdigit;` `            ``// If A is equal to B``            ``if` `(A == B) {``                ``return` `1``;``            ``}``            ``// If A is equal to the initial``            ``// value of integer A``            ``if` `(A == temp) {``                ``return` `0``;``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `A = ``967``, B = ``679``;` `        ``if` `(check(A, B) == ``1``)``            ``System.out.println(``"Yes"``);``        ``else``            ``System.out.println(``"No"``);``    ``}``}` `// This code is contributed by Dharanendra L V.`

## Python3

 `# Python3 implementation of the approach``import` `math` `# Function to check if the integer``# A is a rotation of the integer B``def` `check(A, B) :`` ` `    ``if` `(A ``=``=` `B) :``        ``return` `1`` ` `    ``# Stores the count of digits in A``    ``dig1 ``=` `math.floor(math.log10(A) ``+` `1``)`` ` `    ``# Stores the count of digits in B``    ``dig2 ``=` `math.floor(math.log10(B) ``+` `1``)`` ` `    ``# If dig1 not equal to dig2``    ``if` `(dig1 !``=` `dig2) :``        ``return` `0`` ` `    ``temp ``=` `A`` ` `    ``while` `(``True``) :`` ` `        ``# Stores position of first digit``        ``power ``=` `pow``(``10``, dig1 ``-` `1``)`` ` `        ``# Stores the first digit``        ``firstdigit ``=` `A ``/``/` `power`` ` `        ``# Rotate the digits of the integer``        ``A ``=` `A ``-` `firstdigit ``*` `power``        ``A ``=` `A ``*` `10` `+` `firstdigit`` ` `        ``# If A is equal to B``        ``if` `(A ``=``=` `B) :``            ``return` `1``        ` `        ``# If A is equal to the initial value of integer A``        ``if` `(A ``=``=` `temp) :``            ``return` `0``          ` `          ``# Driver code``A, B ``=` `967``, ``679`` ` `if` `(check(A, B)) :``    ``print``(``"Yes"``)``else` `:``    ``print``(``"No"``)``    ` `    ``# This code is contributed by divyesh072019.`

## C#

 `// C# implementation of the approach``using` `System;``public` `class` `GFG``{` `    ``// Function to check if the integer``    ``// A is a rotation of the integer B``    ``static` `int` `check(``int` `A, ``int` `B)``    ``{``        ``if` `(A == B)``        ``{``            ``return` `1;``        ``}` `        ``// Stores the count of digits in A``        ``int` `dig1 = (``int``)Math.Floor(Math.Log10(A) + 1);` `        ``// Stores the count of digits in B``        ``int` `dig2 = (``int``)Math.Floor(Math.Log10(B) + 1);` `        ``// If dig1 not equal to dig2``        ``if` `(dig1 != dig2)``        ``{``            ``return` `0;``        ``}``        ``int` `temp = A;``        ``while` `(``true``)``        ``{` `            ``// Stores position of first digit``            ``int` `power = (``int``)Math.Pow(10, dig1 - 1);` `            ``// Stores the first digit``            ``int` `firstdigit = A / power;` `            ``// Rotate the digits of the integer``            ``A = A - firstdigit * power;``            ``A = A * 10 + firstdigit;` `            ``// If A is equal to B``            ``if` `(A == B)``            ``{``                ``return` `1;``            ``}``          ` `            ``// If A is equal to the initial``            ``// value of integer A``            ``if` `(A == temp)``            ``{``                ``return` `0;``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `A = 967, B = 679;``        ``if` `(check(A, B) == 1)``            ``Console.WriteLine(``"Yes"``);``        ``else``            ``Console.WriteLine(``"No"``);``    ``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(digit(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up