# Largest value of x such that axx is N-digit number of base b

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 number of digit = 2,
but 2 * 44 = 512 which has 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: This problem can be solved using binary search.

• 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++

 `// C++ implementation of the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find log_b(a) ` `double` `log``(``int` `a, ``int` `b) ` `{ ` `    ``return` `log10``(a) / ``log10``(b); ` `} ` ` `  `int` `get(``int` `a, ``int` `b, ``int` `n) ` `{ ` ` `  `    ``// Set two pointer for binary search ` `    ``int` `lo = 0, hi = 1e6; ` ` `  `    ``int` `ans = 0; ` ` `  `    ``while` `(lo <= hi) { ` `        ``int` `mid = (lo + hi) / 2; ` ` `  `        ``// Calculating number of digits ` `        ``// of a*mid^mid in base b ` `        ``int` `dig = ``ceil``((mid * ``log``(mid, b) ` `                        ``+ ``log``(a, b))); ` ` `  `        ``if` `(dig > n) { ` ` `  `            ``// If number of digits > n ` `            ``// we can simply ignore it ` `            ``// and decrease our pointer ` `            ``hi = mid - 1; ` `        ``} ` `        ``else` `{ ` ` `  `            ``// if number of digits <= n, ` `            ``// we can go higher to ` `            ``// reach value exactly equal to n ` `            ``ans = mid; ` `            ``lo = mid + 1; ` `        ``} ` `    ``} ` ` `  `    ``// return the largest value of x ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``int` `a = 2, b = 2, n = 6; ` ` `  `    ``cout << get(a, b, n) ` `         ``<< ``"\n"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the above approach ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to find log_b(a) ` `static` `int` `log(``int` `a, ``int` `b) ` `{ ` `    ``return` `(``int``)(Math.log10(a) / ` `                 ``Math.log10(b)); ` `} ` ` `  `static` `int` `get(``int` `a, ``int` `b, ``int` `n) ` `{ ` ` `  `    ``// Set two pointer for binary search ` `    ``int` `lo = ``0``, hi = (``int``) 1e6; ` ` `  `    ``int` `ans = ``0``; ` ` `  `    ``while` `(lo <= hi) ` `    ``{ ` `        ``int` `mid = (lo + hi) / ``2``; ` ` `  `        ``// Calculating number of digits ` `        ``// of a*mid^mid in base b ` `        ``int` `dig = (``int``) Math.ceil((mid * log(mid, b) + ` `                                         ``log(a, b))); ` ` `  `        ``if` `(dig > n)  ` `        ``{ ` ` `  `            ``// If number of digits > n ` `            ``// we can simply ignore it ` `            ``// and decrease our pointer ` `            ``hi = mid - ``1``; ` `        ``} ` `        ``else`  `        ``{ ` ` `  `            ``// If number of digits <= n, ` `            ``// we can go higher to reach  ` `            ``// value exactly equal to n ` `            ``ans = mid; ` `            ``lo = mid + ``1``; ` `        ``} ` `    ``} ` ` `  `    ``// Return the largest value of x ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a = ``2``, b = ``2``, n = ``6``; ` ` `  `    ``System.out.print(get(a, b, n) + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

## Python3

 `# Python3 implementation of the above approach ` ` `  `from` `math ``import` `log10,ceil,log ` ` `  `# Function to find log_b(a) ` `def` `log1(a,b): ` `    ``return` `log10(a)``/``/``log10(b) ` ` `  `def` `get(a,b,n): ` `    ``# Set two pointer for binary search ` `    ``lo ``=` `0` `    ``hi ``=` `1e6` ` `  `    ``ans ``=` `0` ` `  `    ``while` `(lo <``=` `hi): ` `        ``mid ``=` `(lo ``+` `hi) ``/``/` `2` ` `  `        ``# Calculating number of digits ` `        ``# of a*mid^mid in base b ` `        ``dig ``=` `ceil((mid ``*` `log(mid, b) ``+` `log(a, b))) ` ` `  `        ``if` `(dig > n): ` `            ``# If number of digits > n ` `            ``# we can simply ignore it ` `            ``# and decrease our pointer ` `            ``hi ``=` `mid ``-` `1` ` `  `        ``else``: ` `            ``# if number of digits <= n, ` `            ``# we can go higher to ` `            ``# reach value exactly equal to n ` `            ``ans ``=` `mid ` `            ``lo ``=` `mid ``+` `1` ` `  `    ``# return the largest value of x ` `    ``return` `ans ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `2` `    ``b ``=` `2` `    ``n ``=` `6` ` `  `    ``print``(``int``(get(a, b, n))) ` ` `  `# This code is contributed by Surendra_Gangwar `

## C#

 `// C# implementation of the above approach ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find log_b(a) ` `static` `int` `log(``int` `a, ``int` `b) ` `{ ` `    ``return` `(``int``)(Math.Log10(a) / ` `                 ``Math.Log10(b)); ` `} ` ` `  `static` `int` `get``(``int` `a, ``int` `b, ``int` `n) ` `{ ` ` `  `    ``// Set two pointer for binary search ` `    ``int` `lo = 0, hi = (``int``) 1e6; ` ` `  `    ``int` `ans = 0; ` ` `  `    ``while` `(lo <= hi) ` `    ``{ ` `        ``int` `mid = (lo + hi) / 2; ` ` `  `        ``// Calculating number of digits ` `        ``// of a*mid^mid in base b ` `        ``int` `dig = (``int``)Math.Ceiling((``double``)(mid *  ` `                                         ``log(mid, b) + ` `                                         ``log(a, b))); ` ` `  `        ``if` `(dig > n)  ` `        ``{ ` ` `  `            ``// If number of digits > n ` `            ``// we can simply ignore it ` `            ``// and decrease our pointer ` `            ``hi = mid - 1; ` `        ``} ` `        ``else` `        ``{ ` ` `  `            ``// If number of digits <= n, ` `            ``// we can go higher to reach  ` `            ``// value exactly equal to n ` `            ``ans = mid; ` `            ``lo = mid + 1; ` `        ``} ` `    ``} ` ` `  `    ``// Return the largest value of x ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `a = 2, b = 2, n = 6; ` ` `  `    ``Console.Write(``get``(a, b, n) + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

Output:

```3
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.