给定一个大小为N的整数arr[]数组,任务是计算数组中频率等于其值的所有元素。
例子:
Input: arr[] = {3, 2, 2, 3, 4, 3}
Output: 2
Explanation :
Frequency of element 2 is 2
Frequency of element 3 is 3
Frequency of element 4 is 1
2 and 3 are elements which have same frequency as it’s value
Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 1
处理方法:按照以下步骤解决问题:
- 将值小于等于给定数组大小的每个数组元素的频率存储在freq[]数组中。
- 执行上述步骤的原因是元素的频率最多可以等于给定数组的大小。因此,不需要存储值大于给定数组大小的元素的频率。
- 计算频率等于其值的整数。
- 打印最终计数。
下面是上述方法的实现:
C++
// C++ program of the
// above approach
#include
using namespace std;
// Function to find the integer
// which has a frequency in the
// array equal to its value.
void solve(int arr[], int n)
{
// Store frequency of array
// elements
int freq[n+1] ={0};
for (int i = 0; i < n; i++) {
// Store the frequency
// only if arr[i]<=n
if (arr[i] <= n)
freq[arr[i]]++;
}
// Initially the count is zero
int count = 0;
for (int i = 1; i <= n; i++) {
// If the freq[i] is equal
// to i, then increment
// the count by 1
if (i == freq[i]) {
count++;
}
}
// Print the final count
cout << count << "\n";
}
// Driver Code
int main()
{
int arr[] = { 3, 1, 1, 3, 2, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
solve(arr, N);
return 0;
}
Java
// Java program of the
// above approach
class GFG{
// Function to find the integer
// which has a frequency in the
// array equal to its value.
static void solve(int arr[],
int n)
{
// Store frequency of array
// elements
int []freq = new int[n + 1];
for (int i = 0; i < n; i++)
{
// Store the frequency
// only if arr[i]<=n
if (arr[i] <= n)
freq[arr[i]]++;
}
// Initially the count is zero
int count = 0;
for (int i = 1; i <= n; i++)
{
// If the freq[i] is equal
// to i, then increment
// the count by 1
if (i == freq[i])
{
count++;
}
}
// Print the final count
System.out.print(count + "\n");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {3, 1, 1, 3, 2, 2, 3};
int N = arr.length;
solve(arr, N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program of the
# above approach
# Function to find the integer
# which has a frequency in the
# array equal to its value.
def solve(arr, n):
# Store frequency of array
# elements
freq = [0] * (n + 1);
for i in range(n):
# Store the frequency
# only if arr[i]<=n
if (arr[i] <= n):
freq[arr[i]] += 1;
# Initially the count is zero
count = 0;
for i in range(1, n + 1):
# If the freq[i] is equal
# to i, then increment
# the count by 1
if (i == freq[i]):
count += 1;
# Print final count
print(count , "");
# Driver Code
if __name__ == '__main__':
arr = [3, 1, 1, 3,
2, 2, 3];
N = len(arr);
solve(arr, N);
# This code is contributed by shikhasingrajput
C#
// C# program of the
// above approach
using System;
class GFG{
// Function to find the integer
// which has a frequency in the
// array equal to its value.
static void solve(int []arr,
int n)
{
// Store frequency of array
// elements
int []freq = new int[n + 1];
for (int i = 0; i < n; i++)
{
// Store the frequency
// only if arr[i]<=n
if (arr[i] <= n)
freq[arr[i]]++;
}
// Initially the count is zero
int count = 0;
for (int i = 1; i <= n; i++)
{
// If the freq[i] is equal
// to i, then increment
// the count by 1
if (i == freq[i])
{
count++;
}
}
// Print the readonly count
Console.Write(count + "\n");
}
// Driver Code
public static void Main(String[] args)
{
int []arr = {3, 1, 1, 3, 2, 2, 3};
int N = arr.Length;
solve(arr, N);
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(N)
请参阅上一篇关于O(N*log(N))方法的文章。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。