Open In App

Javascript Program to Count rotations divisible by 4

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 rotations are : 43292816, 16432928, 81643292
                  92816432, 32928164 

For large numbers it is difficult to rotate and divide each number by 4. Therefore, ‘divisibility by 4’ property is used which says that a number is divisible by 4 if the last 2 digits of the number is divisible by 4. Here we do not actually rotate the number and check last 2 digits for divisibility, instead we count consecutive pairs (in circular way) which are divisible by 4. 

Illustration:  

Consider a number 928160
Its rotations are 928160, 092816, 609281, 160928, 
    816092, 281609.
Now form pairs from the original number 928160
as mentioned in the approach.
Pairs: (9,2), (2,8), (8,1), (1,6), 
         (6,0), (0,9)
We can observe that the 2-digit number formed by the these 
pairs, i.e., 92, 28, 81, 16, 60, 09, are present in the last
2 digits of some rotation.
Thus, checking divisibility of these pairs gives the required
number of rotations. 

Note: A single digit number can directly
be checked for divisibility.

Below is the implementation of the approach. 

Javascript




<script>
 
// Javascript program to count all
// rotation divisible by 4.
 
// Returns count of all
// rotations divisible
// by 4
function countRotations(n)
{
    let len = n.length;
 
    // For single digit number
    if (len == 1)
    {
        let oneDigit = n[0] - '0';
 
        if (oneDigit % 4 == 0)
            return 1;
 
        return 0;
    }
 
    // At-least 2 digit
    // number (considering all
    // pairs)
    let twoDigit;
    let count = 0;
     
    for(let i = 0; i < (len - 1); i++)
    {
        twoDigit = (n[i] - '0') * 10 +
                   (n[i + 1] - '0');
 
        if (twoDigit % 4 == 0)
            count++;
    }
 
    // Considering the number
    // formed by the pair of
    // last digit and 1st digit
    twoDigit = (n[len - 1] - '0') * 10 +
               (n[0] - '0');
 
    if (twoDigit % 4 == 0)
        count++;
 
    return count;
}
 
// Driver Code
let n = "4834";
 
document.write("Rotations: " +
               countRotations(n));
 
// This code is contributed by _saurabh_jaiswal
     
</script>


Output: 

Rotations: 2

Time Complexity : O(n) where n is number of digits in input number.

Auxiliary Space: O(1)

Please refer complete article on Count rotations divisible by 4 for more details!



Similar Reads

Javascript 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 divisible by 10. So 2 is the output. Input: N = 135
2 min read
Javascript Program to Count rotations divisible by 8
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 rotat
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 divisible by 10. So 2 is the output. Input: N = 135
2 min read
Java 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 divisible by 10. So 2 is the output. Input: N = 135
2 min read
Python3 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 divisible by 10. So 2 is the output. Input: N = 135
2 min read
C++ Program to Count rotations divisible by 8
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 rotat
3 min read
Java Program for Count rotations divisible by 8
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 rotat
3 min read
Python3 Program for Count rotations divisible by 8
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 rotat
3 min read
Php Program to Count rotations divisible by 8
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 rotat
3 min read
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 rotations are : 43292816, 16432928, 81643292 92816432
3 min read