给定数组中其索引的 K 次方的元素计数
给定一个具有N个非负整数的数组arr[] ,任务是找到其索引的 K 次方的元素数,其中 K 是非负数。
arr[i] = iK
例子:
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
方法:给定问题可以通过查找每个索引的 K 次方并检查它们是否等于该索引处存在的元素来解决。请按照以下步骤解决问题:
- 将变量res初始化为 0 用于存储答案
- 如果 K 的值为 0:
- 如果数组arr中的第一个元素存在且等于 1,则将res加 1
- 否则,如果 K 的值大于 0:
- 如果数组arr中的第一个元素存在并且等于 0,则将res加 1
- 如果数组arr中的第二个元素存在且等于 1,则将res加 1
- 从索引 2 到末尾以及每个索引处迭代数组arr :
- 将变量indPow初始化为当前索引i并乘以i,k-1次
- 如果indPow的值等于当前元素arr[i]则将res加 1
- 返回res作为我们的答案
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find count of elements which
// are a non-negative power of their indices
int indPowEqualsEle(vector 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 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
//
输出
3
时间复杂度: O(N * K)
辅助空间: O(1)