![]() |
VOOZH | about |
Given two positive integers X and Y, the task is to find the MSB of X, in the given base Y.
Examples:
Input: X = 55, Y = 3
Output: 2
Explanation:
55 is 2001 in base 3 with first digit as 2.
Input: X = 123, Y = 10
Output: 1
Explanation:
123 is 123 in base 10 with first digit 1.
Approach: Let the task to find 1st digit of X = 1234 in base Y = 10, So to get First digit = 1:
Divide 1234 by 1000
= X / 103
= X / 10Number of Digits in X - 1
= X / 10log(X) / log(10) (which is for base 10)
For any other base, we can replace 10 with Y. Therefore, we can calculate the first digit of a number X in base Y by using the formula:
X / Y(log(X)/log(Y))
Below is the implementation of the above approach:
2
Time Complexity: O(1)
Auxiliary Space: O(1)