Given an octal number N, the task is to convert the number to decimal and then find the modulo with every power of 2 i.e. 2i such that i > 0 and 2i < N and print the maximum frequency of the modulo.
Input: N = 13
Octal(13) = decimal(11)
11 % 2 = 1
11 % 4 = 3
11 % 8 = 3
3 occurs the most i.e. 2 times.
Input: N = 21
Approach: Find the binary representation of the number by replacing the digit with their binary representation. Now it is known that every digit in the binary representation represents a power of 2 in increasing order. So the modulo of the number with a power of 2 is the number formed by the binary representation of its preceding bits. For Example,
Octal(13) = decimal(11) = binary(1011)
11(1011) % 2 (10) = 1 (1)
11(1011) % 4 (100) = 3 (11)
11(1011) % 8 (1000) = 3 (011)
Here, it can be observed that when there is a zero in the binary representation of the modulo, the number remains the same. So the maximum frequency of the modulo will be 1 + the number of consecutive 0’s in the binary representation (not the leading zeroes) of the number. As the modulo starts from 2, remove the LSB of the number.
Below is the implementation of the above approach:
- Program to find remainder without using modulo or % operator
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Find permutation with maximum remainder Sum
- Maximum subarray sum modulo m
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Quotient - Remainder Sort
- Remainder with 7 for large numbers
- Quotient and remainder dividing by 2^k (a power of 2)
- Program for quotient and remainder of big number
- Chinese Remainder Theorem | Set 1 (Introduction)
- Compute n! under modulo p
- LCM of N numbers modulo M
- Fibonacci modulo p
- Modulo 10^9+7 (1000000007)
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.