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 its 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++
#include <bits/stdc++.h>
using namespace std;
const string bin[] = { "000" , "001" , "010" , "011" ,
"100" , "101" , "110" , "111" };
int maxFreq(string s)
{
string binary = "" ;
for ( int i = 0; i < s.length(); i++) {
binary += bin[s[i] - '0' ];
}
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 (binary[i] == '1' ) {
count = max(count, j - prev);
prev = j;
}
return count;
}
int main()
{
string octal = "13" ;
cout << maxFreq(octal);
return 0;
}
|
Java
class GFG
{
static String bin[] = { "000" , "001" , "010" , "011" ,
"100" , "101" , "110" , "111" };
static int maxFreq(String s)
{
String binary = "" ;
for ( int i = 0 ; i < s.length(); i++)
{
binary += bin[s.charAt(i) - '0' ];
}
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 (binary.charAt(i) == '1' )
{
count = Math.max(count, j - prev);
prev = j;
}
return count;
}
public static void main(String []args)
{
String octal = "13" ;
System.out.println(maxFreq(octal));
}
}
|
Python3
bin = [ "000" , "001" , "010" , "011" ,
"100" , "101" , "110" , "111" ];
def maxFreq(s) :
binary = "";
for i in range ( len (s)) :
binary + = bin [ ord (s[i]) - ord ( '0' )];
binary = binary[ 0 : len (binary) - 1 ];
count = 1 ; prev = - 1 ;j = 0 ;
for i in range ( len (binary) - 1 , - 1 , - 1 ) :
if (binary[i] = = '1' ) :
count = max (count, j - prev);
prev = j;
j + = 1 ;
return count;
if __name__ = = "__main__" :
octal = "13" ;
print (maxFreq(octal));
|
C#
using System;
class GFG
{
static String []bin = { "000" , "001" , "010" , "011" ,
"100" , "101" , "110" , "111" };
static int maxFreq(String s)
{
String binary = "" ;
for ( int K = 0; K < s.Length; K++)
{
binary += bin[s[K] - '0' ];
}
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 (binary[i] == '1' )
{
count = Math.Max(count, j - prev);
prev = j;
}
return count;
}
public static void Main(String []args)
{
String octal = "13" ;
Console.WriteLine(maxFreq(octal));
}
}
|
Javascript
<script>
bin = [ "000" , "001" , "010" , "011" ,
"100" , "101" , "110" , "111" ];
function maxFreq( s )
{
var binary = "" ;
for ( var i = 0; i < s.length; i++) {
binary += bin[s.charAt(i) - '0' ];
}
binary = binary.substr(0, binary.length - 1);
var count = 1, prev = -1, i, j = 0;
for (i = binary.length - 1; i >= 0; i--, j++)
if (binary.charAt(i) == '1' ) {
count = Math.max(count, j - prev);
prev = j;
}
return count;
}
var octal = "13" ;
document.write( maxFreq(octal));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the length of the string.
Auxiliary Space: O(N), as we are using extra space for the string binary.
Approach:
One simple way is to convert the octal number to decimal and then calculate the modulo with every power of 2 until 2i becomes greater than or equal to the decimal value. We can use an array to store the frequency of each modulo and then find the maximum frequency.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int octalToDecimal(string octal)
{
int decimal = 0, power = 1;
for ( int i = octal.length() - 1; i >= 0; i--) {
decimal += (octal[i] - '0' ) * power;
power *= 8;
}
return decimal;
}
int maxFreq(string octal)
{
int decimal = octalToDecimal(octal);
int freq[32]
= { 0 };
for ( int i = 1; i <= log2(decimal); i++) {
int rem = decimal % (1 << i);
freq[rem]++;
}
int maxFreq = 0, iMax = 0;
for ( int i = 0; i <= log2(decimal); i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
iMax = i;
}
}
return maxFreq;
}
int main()
{
string octal = "13" ;
cout << maxFreq(octal);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
public static int octalToDecimal(String octal)
{
int decimal = 0 , power = 1 ;
for ( int i = octal.length() - 1 ; i >= 0 ; i--) {
decimal += (octal.charAt(i) - '0' ) * power;
power *= 8 ;
}
return decimal;
}
public static int maxFreq(String octal)
{
int decimal = octalToDecimal(octal);
int [] freq = new int [ 32 ];
Arrays.fill(freq, 0 );
for ( int i = 1 ;
i <= Math.log(decimal) / Math.log( 2 ); i++) {
int rem = decimal % ( 1 << i);
freq[rem]++;
}
int maxFreq = 0 , iMax = 0 ;
for ( int i = 0 ;
i <= Math.log(decimal) / Math.log( 2 ); i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
iMax = i;
}
}
return maxFreq;
}
public static void main(String[] args)
{
String octal = "13" ;
System.out.println(maxFreq(octal));
}
}
|
Python
import math
def octal_to_decimal(octal):
decimal_num = 0
power = 1
for i in range ( len (octal) - 1 , - 1 , - 1 ):
decimal_num + = int (octal[i]) * power
power * = 8
return decimal_num
def max_freq(octal):
decimal_num = octal_to_decimal(octal)
freq = [ 0 ] * 32
for i in range ( 1 , int (math.log(decimal_num, 2 )) + 1 ):
rem = decimal_num % ( 1 << i)
freq[rem] + = 1
max_freq = 0
i_max = 0
for i in range ( int (math.log(decimal_num, 2 )) + 1 ):
if freq[i] > max_freq:
max_freq = freq[i]
i_max = i
return max_freq
if __name__ = = "__main__" :
octal = "13"
print (max_freq(octal))
|
C#
using System;
class Program {
static int OctalToDecimal( string octal)
{
int decimalNum = 0;
int power = 1;
for ( int i = octal.Length - 1; i >= 0; i--) {
decimalNum += (octal[i] - '0' ) * power;
power *= 8;
}
return decimalNum;
}
static int MaxFreq( string octal)
{
int decimalNum = OctalToDecimal(octal);
int [] freq = new int [32];
for ( int i = 1;
i <= ( int )(Math.Log(decimalNum) / Math.Log(2));
i++) {
int rem = decimalNum % (1 << i);
freq[rem]++;
}
int maxFreq = 0;
int iMax = 0;
for ( int i = 0;
i <= ( int )(Math.Log(decimalNum) / Math.Log(2));
i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
iMax = i;
}
}
Console.WriteLine( "Max frequency: " + maxFreq);
Console.WriteLine( "iMax: " + iMax);
return maxFreq;
}
static void Main()
{
string octal = "13" ;
Console.WriteLine(MaxFreq(octal));
}
}
|
Javascript
function octalToDecimal(octal) {
let decimal = 0;
let power = 1;
for (let i = octal.length - 1; i >= 0; i--) {
decimal += (parseInt(octal[i], 10)) * power;
power *= 8;
}
return decimal;
}
function maxFreq(octal) {
let decimal = octalToDecimal(octal);
let freq = new Array(32).fill(0);
for (let i = 1; i <= Math.log2(decimal); i++) {
let rem = decimal % (1 << i);
freq[rem]++;
}
let maxFreq = 0;
let iMax = 0;
for (let i = 0; i <= Math.log2(decimal); i++) {
if (freq[i] > maxFreq) {
maxFreq = freq[i];
iMax = i;
}
}
return maxFreq;
}
let octal = "13" ;
console.log(maxFreq(octal));
|
Time Complexity: O(log n), where n is the decimal equivalent of the given octal number. The loop runs for log2(n) times to calculate the modulos with powers of 2.
Auxiliary Space: O(log n), where n is the decimal equivalent of the given octal number. This is because we need to store the frequency of modulos in an array of size log2(n) + 1.