Count of elements that are Kth powers of their indices in given Array
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:
- Initialize a variable res to 0 for storing the answer
- If the value of K is 0:
- If the first element in the array arr exists and is equal to 1 then increment res by 1
- Else if the value of K is greater than 0:
- If the first element in the array arr exists and is equal to 0 then increment res by 1
- If the second element in the array arr exists and is equal to 1 then increment res by 1
- Iterate the array arr from index 2 till the end and at every index:
- Initialize a variable indPow to the current index i and multiply it by i, k-1 times
- If the value of indPow becomes equal to the current element arr[i] then increment res by 1
- Return res as our answer
Below is the implementation of the above approach:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to find count of elements which // are a non-negative power of their indices int indPowEqualsEle(vector< int > arr, int K) { // Length of the array int len = arr.size(); // Initialize variable res for // calculating the answer int res = 0; // If the value of K is 0 if (K == 0) { // If the first element is 1 // then the condition is satisfied if (len > 0 && arr[0] == 1) res++; } // If the value of K > 0 if (K > 0) { // If the first is 0 increment res if (len > 0 && arr[0] == 1) res++; } // If the second element is 1 // then increment res if (len > 1 && arr[1] == 1) res++; // Iterate the array arr from index 2 for ( int i = 2; i < len; i++) { // Initialize a variable // to index which is to be // multiplied K-1 times long indPow = i; for ( int j = 2; j < K; j++) { // Multiply current value // with index K - 1 times indPow *= i; } // If index power K is equal to // the element then increment res if (indPow == arr[i]) res++; } // return the result return res; } // Driver function int main() { // Initialize an array vector< int > arr = {1, 1, 4, 3, 16, 125, 1}; // Initialize power int K = 0; // Call the function // and print the answer cout << (indPowEqualsEle(arr, K)); return 0; } // This code is contributed by Potta Lokesh |
Java
// Java implementation for the above approach import java.io.*; import java.util.*; class GFG { // Function to find count of elements which // are a non-negative power of their indices public static int indPowEqualsEle( int [] arr, int K) { // Length of the array int len = arr.length; // Initialize variable res for // calculating the answer int res = 0 ; // If the value of K is 0 if (K == 0 ) { // If the first element is 1 // then the condition is satisfied if (len > 0 && arr[ 0 ] == 1 ) res++; } // If the value of K > 0 if (K > 0 ) { // If the first is 0 increment res if (len > 0 && arr[ 0 ] == 1 ) res++; } // If the second element is 1 // then increment res if (len > 1 && arr[ 1 ] == 1 ) res++; // Iterate the array arr from index 2 for ( int i = 2 ; i < len; i++) { // Initialize a variable // to index which is to be // multiplied K-1 times long indPow = i; for ( int j = 2 ; j < K; j++) { // Multiply current value // with index K - 1 times indPow *= i; } // If index power K is equal to // the element then increment res if (indPow == arr[i]) res++; } // return the result return res; } // Driver function public static void main(String[] args) { // Initialize an array int [] arr = { 1 , 1 , 4 , 3 , 16 , 125 , 1 }; // Initialize power int K = 0 ; // Call the function // and print the answer System.out.println( indPowEqualsEle(arr, K)); } } |
Python3
# python code for the above approach # Function to find count of elements which # are a non-negative power of their indices def indPowEqualsEle(arr, K): # Length of the array le = len (arr) # Initialize variable res for # calculating the answer res = 0 # If the value of K is 0 if (K = = 0 ): # If the first element is 1 # then the condition is satisfied if (le > 0 and arr[ 0 ] = = 1 ): res + = 1 # If the value of K > 0 if (K > 0 ): # If the first is 0 increment res if (le > 0 and arr[ 0 ] = = 1 ): res + = 1 # If the second element is 1 # then increment res if (le > 1 and arr[ 1 ] = = 1 ): res + = 1 # Iterate the array arr from index 2 for i in range ( 2 , le): # Initialize a variable # to index which is to be # multiplied K-1 times indPow = i for j in range ( 2 , K): # Multiply current value # with index K - 1 times indPow * = i # If index power K is equal to # the element then increment res if (indPow = = arr[i]): res + = 1 # return the result return res # Driver function if __name__ = = "__main__" : # Initialize an array arr = [ 1 , 1 , 4 , 3 , 16 , 125 , 1 ] # Initialize power K = 0 # Call the function # and print the answer print (indPowEqualsEle(arr, K)) # This code is contributed by rakeshsahni |
C#
// C# program for the above approach using System; using System.Collections; class GFG { // Function to find count of elements which // are a non-negative power of their indices static int indPowEqualsEle( int []arr, int K) { // Length of the array int len = arr.Length; // Initialize variable res for // calculating the answer int res = 0; // If the value of K is 0 if (K == 0) { // If the first element is 1 // then the condition is satisfied if (len > 0 && arr[0] == 1) res++; } // If the value of K > 0 if (K > 0) { // If the first is 0 increment res if (len > 0 && arr[0] == 1) res++; } // If the second element is 1 // then increment res if (len > 1 && arr[1] == 1) res++; // Iterate the array arr from index 2 for ( int i = 2; i < len; i++) { // Initialize a variable // to index which is to be // multiplied K-1 times long indPow = i; for ( int j = 2; j < K; j++) { // Multiply current value // with index K - 1 times indPow *= i; } // If index power K is equal to // the element then increment res if (indPow == arr[i]) res++; } // return the result return res; } // Driver Code public static void Main() { // Initialize an array int []arr = {1, 1, 4, 3, 16, 125, 1}; // Initialize power int K = 0; // Call the function // and print the answer Console.Write(indPowEqualsEle(arr, K)); } } // This code is contributed by Samim Hossain Mondal |
Javascript
//<script> // Javascript program for the above approach // Function to find count of elements which // are a non-negative power of their indices function indPowEqualsEle(arr, K) { // Length of the array let len = arr.length; // Initialize variable res for // calculating the answer let res = 0; // If the value of K is 0 if (K == 0) { // If the first element is 1 // then the condition is satisfied if (len > 0 && arr[0] == 1) res++; } // If the value of K > 0 if (K > 0) { // If the first is 0 increment res if (len > 0 && arr[0] == 1) res++; } // If the second element is 1 // then increment res if (len > 1 && arr[1] == 1) res++; // Iterate the array arr from index 2 for (let i = 2; i < len; i++) { // Initialize a variable // to index which is to be // multiplied K-1 times let indPow = i; for (let j = 2; j < K; j++) { // Multiply current value // with index K - 1 times indPow *= i; } // If index power K is equal to // the element then increment res if (indPow == arr[i]) res++; } // return the result return res; } // Driver Code // Initialize an array let arr = [ 1, 1, 4, 3, 16, 125, 1 ]; // Initialize power let K = 0; // Call the function // and print the answer document.write(indPowEqualsEle(arr, K)); // This code is contributed by Samim Hossain Mondal </script> |
Output
3
Time Complexity: O(N * K)
Auxiliary Space: O(1)