Maximum frequency of a remainder modulo 2i

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.

Examples:

Input: N = 13
Output: 2
Octal(13) = decimal(11)
11 % 2 = 1
11 % 4 = 3
11 % 8 = 3
3 occurs the most i.e. 2 times.

Input: N = 21
Output: 4

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)
So,
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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Binary representation of the digits
const string bin[] = { "000", "001", "010", "011",
                       "100", "101", "110", "111" };
  
// Function to return the maximum frequency
// of s modulo with a power of 2
int maxFreq(string s)
{
  
    // Store the binary representation
    string binary = "";
  
    // Convert the octal to binary
    for (int i = 0; i < s.length(); i++) {
        binary += bin[s[i] - '0'];
    }
  
    // Remove the LSB
    binary = binary.substr(0, binary.length() - 1);
  
    int count = 1, prev = -1, i, j = 0;
  
    for (i = binary.length() - 1; i >= 0; i--, j++)
  
        // If there is 1 in the binary representation
        if (binary[i] == '1') {
  
            // Find the number of zeroes in between
            // two 1's in the binary representation
            count = max(count, j - prev);
            prev = j;
        }
  
    return count;
}
  
// Driver code
int main()
{
    string octal = "13";
  
    cout << maxFreq(octal);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Binary representation of the digits
static String bin[] = { "000", "001", "010", "011",
                        "100", "101", "110", "111" };
  
// Function to return the maximum frequency
// of s modulo with a power of 2
static int maxFreq(String s)
{
  
    // Store the binary representation
    String binary = "";
  
    // Convert the octal to binary
    for (int i = 0; i < s.length(); i++) 
    {
        binary += bin[s.charAt(i) - '0'];
    }
  
    // Remove the LSB
    binary = binary.substring(0
             binary.length() - 1);
  
    int count = 1, prev = -1, i, j = 0;
  
    for (i = binary.length() - 1
         i >= 0; i--, j++)
  
        // If there is 1 in the binary representation
        if (binary.charAt(i) == '1')
        {
  
            // Find the number of zeroes in between
            // two 1's in the binary representation
            count = Math.max(count, j - prev);
            prev = j;
        }
    return count;
}
  
// Driver code
public static void main(String []args)
{
    String octal = "13";
  
    System.out.println(maxFreq(octal));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Binary representation of the digits 
bin = [ "000", "001", "010", "011"
        "100", "101", "110", "111" ]; 
  
# Function to return the maximum frequency 
# of s modulo with a power of 2 
def maxFreq(s) :
  
    # Store the binary representation 
    binary = ""; 
  
    # Convert the octal to binary 
    for i in range(len(s)) :
        binary += bin[ord(s[i]) - ord('0')]; 
      
    # Remove the LSB 
    binary = binary[0 : len(binary) - 1]; 
  
    count = 1; prev = -1;j = 0
  
    for i in range(len(binary) - 1, -1, -1) :
  
        # If there is 1 in the binary representation 
        if (binary[i] == '1') :
  
            # Find the number of zeroes in between 
            # two 1's in the binary representation 
            count = max(count, j - prev); 
            prev = j;
          
        j += 1;
  
    return count; 
  
# Driver code 
if __name__ == "__main__"
  
    octal = "13"
  
    print(maxFreq(octal)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
                      
class GFG
{
  
// Binary representation of the digits
static String []bin = { "000", "001", "010", "011",
                        "100", "101", "110", "111" };
  
// Function to return the maximum frequency
// of s modulo with a power of 2
static int maxFreq(String s)
{
  
    // Store the binary representation
    String binary = "";
  
    // Convert the octal to binary
    for (int K = 0; K < s.Length; K++) 
    {
        binary += bin[s[K] - '0'];
    }
  
    // Remove the LSB
    binary = binary.Substring(0, 
             binary.Length - 1);
  
    int count = 1, prev = -1, i, j = 0;
  
    for (i = binary.Length - 1; 
         i >= 0; i--, j++)
  
        // If there is 1 in the binary representation
        if (binary[i] == '1')
        {
      
            // Find the number of zeroes in between
            // two 1's in the binary representation
            count = Math.Max(count, j - prev);
            prev = j;
        }
    return count;
}
  
// Driver code
public static void Main(String []args)
{
    String octal = "13";
  
    Console.WriteLine(maxFreq(octal));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Third year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : AnkitRai01, 29AjayKumar