![]() |
VOOZH | about |
Given two integers a and b, the task is to find the log of a to any base b, i.e. logb a.
Examples:
Input: a = 3, b = 2
Output: 1
Input: a = 256, b = 4
Output: 4
Below is the implementation of the above approach
1 4
Time Complexity: O(logba)
Auxiliary Space: O(1)
Below is the implementation of the above approach
1 4
Time Complexity: O(logba)
Auxiliary Space: O(logba) Space required for recursive call stack
Approach:
The approach is to repeatedly divide the given number 'a' by the base 'b' until 'a' becomes less than or equal to 1. In each division, we increment the answer by 1. Finally, we return the answer which represents the logarithm of 'a' to the base 'b'.
1 4
Time Complexity: O(log a), as in each iteration, 'a' is divided by 'b'.
Space Complexity: O(1), as only a constant amount of extra space is required for storing the 'ans' variable.