Count of elements that are Kth powers of their indices in given Array
Last Updated :
02 Feb, 2022
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 1
Input: 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++
#include <bits/stdc++.h>
using namespace std;
int indPowEqualsEle(vector< int > arr, int K)
{
int len = arr.size();
int res = 0;
if (K == 0)
{
if (len > 0 && arr[0] == 1)
res++;
}
if (K > 0)
{
if (len > 0 && arr[0] == 1)
res++;
}
if (len > 1 && arr[1] == 1)
res++;
for ( int i = 2; i < len; i++)
{
long indPow = i;
for ( int j = 2; j < K; j++)
{
indPow *= i;
}
if (indPow == arr[i])
res++;
}
return res;
}
int main()
{
vector< int > arr = {1, 1, 4, 3, 16, 125, 1};
int K = 0;
cout << (indPowEqualsEle(arr, K));
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int
indPowEqualsEle( int [] arr, int K)
{
int len = arr.length;
int res = 0 ;
if (K == 0 ) {
if (len > 0 && arr[ 0 ] == 1 )
res++;
}
if (K > 0 ) {
if (len > 0 && arr[ 0 ] == 1 )
res++;
}
if (len > 1 && arr[ 1 ] == 1 )
res++;
for ( int i = 2 ; i < len; i++) {
long indPow = i;
for ( int j = 2 ; j < K; j++) {
indPow *= i;
}
if (indPow == arr[i])
res++;
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 1 , 1 , 4 , 3 , 16 , 125 , 1 };
int K = 0 ;
System.out.println(
indPowEqualsEle(arr, K));
}
}
|
Python3
def indPowEqualsEle(arr, K):
le = len (arr)
res = 0
if (K = = 0 ):
if (le > 0 and arr[ 0 ] = = 1 ):
res + = 1
if (K > 0 ):
if (le > 0 and arr[ 0 ] = = 1 ):
res + = 1
if (le > 1 and arr[ 1 ] = = 1 ):
res + = 1
for i in range ( 2 , le):
indPow = i
for j in range ( 2 , K):
indPow * = i
if (indPow = = arr[i]):
res + = 1
return res
if __name__ = = "__main__" :
arr = [ 1 , 1 , 4 , 3 , 16 , 125 , 1 ]
K = 0
print (indPowEqualsEle(arr, K))
|
C#
using System;
using System.Collections;
class GFG
{
static int indPowEqualsEle( int []arr, int K)
{
int len = arr.Length;
int res = 0;
if (K == 0)
{
if (len > 0 && arr[0] == 1)
res++;
}
if (K > 0)
{
if (len > 0 && arr[0] == 1)
res++;
}
if (len > 1 && arr[1] == 1)
res++;
for ( int i = 2; i < len; i++)
{
long indPow = i;
for ( int j = 2; j < K; j++)
{
indPow *= i;
}
if (indPow == arr[i])
res++;
}
return res;
}
public static void Main()
{
int []arr = {1, 1, 4, 3, 16, 125, 1};
int K = 0;
Console.Write(indPowEqualsEle(arr, K));
}
}
|
Javascript
function indPowEqualsEle(arr, K)
{
let len = arr.length;
let res = 0;
if (K == 0)
{
if (len > 0 && arr[0] == 1)
res++;
}
if (K > 0)
{
if (len > 0 && arr[0] == 1)
res++;
}
if (len > 1 && arr[1] == 1)
res++;
for (let i = 2; i < len; i++)
{
let indPow = i;
for (let j = 2; j < K; j++)
{
indPow *= i;
}
if (indPow == arr[i])
res++;
}
return res;
}
let arr = [ 1, 1, 4, 3, 16, 125, 1 ];
let K = 0;
document.write(indPowEqualsEle(arr, K));
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...