Largest value of x such that axx is N-digit number of base b
Last Updated :
29 Dec, 2022
Given the integers a, b, N, the task is to find the largest number x such that is an N-digit number of base b.
Examples:
Input: a = 2, b = 10, N = 2
Output: 3
Explanation:
Here 2 * 33 = 54, which has the number of digits = 2,
but 2 * 44 = 512 which has a number of digits = 3, which is not equal to N.
Therefore, the largest value of x is 2.
Input: a = 1, b = 2, N = 3
Output: 2
Explanation:
1 * 22 = 4 whose binary representation is 100 and it has 3 digits.
Approach: This problem can be solved using binary search.
- The number of digits of in base is .
- Binary search is used to find the largest such that the number of digits of in base is exactly .
- In binary search, we will check the number of digits , where , and change the pointer according to that.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double log ( int a, int b)
{
return log10 (a) / log10 (b);
}
int get( int a, int b, int n)
{
int lo = 0, hi = 1e6;
int ans = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
int dig = ceil ((mid * log (mid, b)
+ log (a, b)));
if (dig > n) {
hi = mid - 1;
}
else {
ans = mid;
lo = mid + 1;
}
}
return ans;
}
int main()
{
int a = 2, b = 2, n = 6;
cout << get(a, b, n)
<< "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int log( int a, int b)
{
return ( int )(Math.log10(a) /
Math.log10(b));
}
static int get( int a, int b, int n)
{
int lo = 0 , hi = ( int ) 1e6;
int ans = 0 ;
while (lo <= hi)
{
int mid = (lo + hi) / 2 ;
int dig = ( int ) Math.ceil((mid * log(mid, b) +
log(a, b)));
if (dig > n)
{
hi = mid - 1 ;
}
else
{
ans = mid;
lo = mid + 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int a = 2 , b = 2 , n = 6 ;
System.out.print(get(a, b, n) + "\n" );
}
}
|
Python3
from math import log10,ceil,log
def log1(a,b):
return log10(a) / / log10(b)
def get(a,b,n):
lo = 0
hi = 1e6
ans = 0
while (lo < = hi):
mid = (lo + hi) / / 2
dig = ceil((mid * log(mid, b) + log(a, b)))
if (dig > n):
hi = mid - 1
else :
ans = mid
lo = mid + 1
return ans
if __name__ = = '__main__' :
a = 2
b = 2
n = 6
print ( int (get(a, b, n)))
|
C#
using System;
class GFG{
static int log( int a, int b)
{
return ( int )(Math.Log10(a) /
Math.Log10(b));
}
static int get ( int a, int b, int n)
{
int lo = 0, hi = ( int ) 1e6;
int ans = 0;
while (lo <= hi)
{
int mid = (lo + hi) / 2;
int dig = ( int )Math.Ceiling(( double )(mid *
log(mid, b) +
log(a, b)));
if (dig > n)
{
hi = mid - 1;
}
else
{
ans = mid;
lo = mid + 1;
}
}
return ans;
}
public static void Main(String[] args)
{
int a = 2, b = 2, n = 6;
Console.Write( get (a, b, n) + "\n" );
}
}
|
Javascript
<script>
function log(a, b)
{
return (Math.log10(a) /
Math.log10(b));
}
function get(a, b, n)
{
let lo = 0, hi = 1e6;
let ans = 0;
while (lo <= hi)
{
let mid = Math.floor((lo + hi) / 2);
let dig = Math.ceil((mid * log(mid, b) +
log(a, b)));
if (dig > n)
{
hi = mid - 1;
}
else
{
ans = mid;
lo = mid + 1;
}
}
return ans;
}
let a = 2, b = 2, n = 6;
document.write(get(a, b, n) + "\n" );
</script>
|
Time Complexity: O(log(k)) where k=1e6.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...