C++ Program to Count rotations divisible by 8
Last Updated :
09 Jun, 2022
Given a large positive number as string, count all rotations of the given number which are divisible by 8.
Examples:
Input: 8
Output: 1
Input: 40
Output: 1
Rotation: 40 is divisible by 8
04 is not divisible by 8
Input : 13502
Output : 0
No rotation is divisible by 8
Input : 43262488612
Output : 4
Approach: For large numbers it is difficult to rotate and divide each number by 8. Therefore, ‘divisibility by 8’ property is used which says that a number is divisible by 8 if the last 3 digits of the number is divisible by 8. Here we do not actually rotate the number and check last 8 digits for divisibility, instead we count consecutive sequence of 3 digits (in circular way) which are divisible by 8.
Illustration:
Consider a number 928160
Its rotations are 928160, 092816, 609281,
160928, 816092, 281609.
Now form consecutive sequence of 3-digits from
the original number 928160 as mentioned in the
approach.
3-digit: (9, 2, 8), (2, 8, 1), (8, 1, 6),
(1, 6, 0),(6, 0, 9), (0, 9, 2)
We can observe that the 3-digit number formed by
the these sets, i.e., 928, 281, 816, 160, 609, 092,
are present in the last 3 digits of some rotation.
Thus, checking divisibility of these 3-digit numbers
gives the required number of rotations.
C++
// C++ program to count all rotations divisible
// by 8
#include <bits/stdc++.h>
using namespace std;
// function to count of all rotations divisible
// by 8
int countRotationsDivBy8(string n)
{
int len = n.length();
int count = 0;
// For single digit number
if (len == 1) {
int oneDigit = n[0] - '0';
if (oneDigit % 8 == 0)
return 1;
return 0;
}
// For two-digit numbers (considering all
// pairs)
if (len == 2) {
// first pair
int first = (n[0] - '0') * 10 + (n[1] - '0');
// second pair
int second = (n[1] - '0') * 10 + (n[0] - '0');
if (first % 8 == 0)
count++;
if (second % 8 == 0)
count++;
return count;
}
// considering all three-digit sequences
int threeDigit;
for (int i = 0; i < (len - 2); i++) {
threeDigit = (n[i] - '0') * 100 +
(n[i + 1] - '0') * 10 +
(n[i + 2] - '0');
if (threeDigit % 8 == 0)
count++;
}
// Considering the number formed by the
// last digit and the first two digits
threeDigit = (n[len - 1] - '0') * 100 +
(n[0] - '0') * 10 +
(n[1] - '0');
if (threeDigit % 8 == 0)
count++;
// Considering the number formed by the last
// two digits and the first digit
threeDigit = (n[len - 2] - '0') * 100 +
(n[len - 1] - '0') * 10 +
(n[0] - '0');
if (threeDigit % 8 == 0)
count++;
// required count of rotations
return count;
}
// Driver program to test above
int main()
{
string n = "43262488612";
cout << "Rotations: "
<< countRotationsDivBy8(n);
return 0;
}
Output:
Rotations: 4
Time Complexity: O(n), where n is the number of digits in the input number.
Auxiliary Space: O(1)
Please refer complete article on Count rotations divisible by 8 for more details!
Similar Reads
C++ Program to Count rotations divisible by 4 Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8 Output: 1 Input: 20 Output: 1 Rotation: 20 is divisible by 4 02 is not divisible by 4 Input : 13502 Output : 0 No rotation is divisible by 4 Input : 43292816 Output : 5 5 rot
3 min read
C++ Program to Count rotations which are divisible by 10 Given a number N, the task is to count all the rotations of the given number which are divisible by 10.Examples: Input: N = 10203 Output: 2 Explanation: There are 5 rotations possible for the given number. They are: 02031, 20310, 03102, 31020, 10203 Out of these rotations, only 20310 and 31020 are d
2 min read
C++ Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
3 min read
C++ Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read
C++ Program to Rotate bits of a number Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put ba
3 min read