![]() |
VOOZH | about |
Given an array arr[] with N non-negative integers, the task is to find the number of elements that are Kth powers of their indices, where K is a non-negative number.
arr[i] = iK
Example:
Input: arr = [1, 1, 4, 3, 16, 125, 1], K = 0
Output: 3
Explanation: 3 elements are Kth powers of their indices:
00 is 1, 10 is 1, and 60 is 1Input: arr = [0, 1, 4, 3], K = 2
Output: 2
Explanation: 02 is 0, 12 is 1, and 22 is 4
Approach: Given problem can be solved by finding the Kth powers of every index and checking if they are equal to the element present at that index. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
3
Time Complexity: O(N * K)
Auxiliary Space: O(1)