![]() |
VOOZH | about |
Given an array B, and two integers N, X (2 ≤ X ≤ 100). Initially, array arr has all its values as zero. The task is to check if array arr[] can be made equal to the given array B after performing the given operations:
Examples:
Input: N = 3, X = 9
B[] = {0, 59059, 810}
Output: YES
Explanation: Initially all values in arr[] are 0. arr[] = {0, 0, 0}
Choose arr[3], and add 92and 93 in two consecutive operations making it equal to 810. (arr[] = {0, 0, 810}).
Choose arr[2] and add 95 to it making it 59059.
Thus now the entire array arr = {0, 59059, 810} which is equal to array b.Input: N = 2, X = 10
B[] = {0, 0}
Output: YES
Explanation: The array initially is in the given stage. No need to increment any value.
Approach: As the question is dealing with the power of X, the first thing to do is for each element in the array B, find its representation in base X.
Below is the implementation of the above approach:
YES
Time Complexity: O(N)
Auxiliary Space: O(1)