
You are given three non-negative integers N, M, and K. Your task is to print the Kth digit from the right in ‘N’ raised to the power ‘M’ that is, in N ^ M.
Note:
1) It is guaranteed that the Kth digit from the right always exists.
2) It is also guaranteed that 'K' is always less than or equal to the number of digits in N ^ M.
3) 'N' and 'M 'can’t be a 0 simultaneously.
The first line contains an integer ‘T', which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains three non-negative integers N, M, and K, as described in the problem statement.
Output Format:
For each test case, print in a new line, an integer denoting the Kth digit in N^M from the right.
The output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
0 <= N <= 15
0 <= M <= 15
1 <= K <= Digits in N ^ M
Time Limit: 1sec.
1
2 4 1
6
2 ^ 4 = 16, and the 1st digit in 16 from the right is 6.
1
3 3 2
2
3^3 = 27, and the 2nd digit in 27 from the right is 2.
Think of computing directly N ^ M and find its Kth digit from the right.
The idea here is to use the pow function to find N raised to the power M. After finding the power, we start removing the digits from the last until we get the kth digit.
O(log(N ^ M)), where ‘N’ and ‘M’ are the non-negative numbers given in the problem.
In the worst case, we will be iterating through all the digits of the N ^ M. Hence, the time complexity is O(log (N ^ M)).
O(1).
As we are using only constant space.