给定一个由 n 个非负整数组成的数组。任务是在 array[] 的任意范围内找到特定元素的频率。范围作为数组中的位置(不是基于 0 的索引)给出。可以有多个给定类型的查询。
例子:
Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
left = 2, right = 8, element = 8
left = 2, right = 5, element = 6
Output : 3
1
The element 8 appears 3 times in arr[left-1..right-1]
The element 6 appears 1 time in arr[left-1..right-1]
幼稚的方法:是从左到右遍历并在找到元素时更新计数变量。
以下是 Naive 方法的代码:-
C++
// C++ program to find total count of an element
// in a range
#include
using namespace std;
// Returns count of element in arr[left-1..right-1]
int findFrequency(int arr[], int n, int left,
int right, int element)
{
int count = 0;
for (int i=left-1; i<=right; ++i)
if (arr[i] == element)
++count;
return count;
}
// Driver Code
int main()
{
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
int n = sizeof(arr) / sizeof(arr[0]);
// Print frequency of 2 from position 1 to 6
cout << "Frequency of 2 from 1 to 6 = "
<< findFrequency(arr, n, 1, 6, 2) << endl;
// Print frequency of 8 from position 4 to 9
cout << "Frequency of 8 from 4 to 9 = "
<< findFrequency(arr, n, 4, 9, 8);
return 0;
}
Java
// JAVA Code to find total count of an element
// in a range
class GFG {
// Returns count of element in arr[left-1..right-1]
public static int findFrequency(int arr[], int n,
int left, int right,
int element)
{
int count = 0;
for (int i = left - 1; i < right; ++i)
if (arr[i] == element)
++count;
return count;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
int n = arr.length;
// Print frequency of 2 from position 1 to 6
System.out.println("Frequency of 2 from 1 to 6 = " +
findFrequency(arr, n, 1, 6, 2));
// Print frequency of 8 from position 4 to 9
System.out.println("Frequency of 8 from 4 to 9 = " +
findFrequency(arr, n, 4, 9, 8));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Python program to find total
# count of an element in a range
# Returns count of element
# in arr[left-1..right-1]
def findFrequency(arr, n, left, right, element):
count = 0
for i in range(left - 1, right):
if (arr[i] == element):
count += 1
return count
# Driver Code
arr = [2, 8, 6, 9, 8, 6, 8, 2, 11]
n = len(arr)
# Print frequency of 2 from position 1 to 6
print("Frequency of 2 from 1 to 6 = ",
findFrequency(arr, n, 1, 6, 2))
# Print frequency of 8 from position 4 to 9
print("Frequency of 8 from 4 to 9 = ",
findFrequency(arr, n, 4, 9, 8))
# This code is contributed by Anant Agarwal.
C#
// C# Code to find total count
// of an element in a range
using System;
class GFG {
// Returns count of element
// in arr[left-1..right-1]
public static int findFrequency(int []arr, int n,
int left, int right,
int element)
{
int count = 0;
for (int i = left - 1; i < right; ++i)
if (arr[i] == element)
++count;
return count;
}
// Driver Code
public static void Main()
{
int []arr = {2, 8, 6, 9, 8, 6, 8, 2, 11};
int n = arr.Length;
// Print frequency of 2
// from position 1 to 6
Console.WriteLine("Frequency of 2 from 1 to 6 = " +
findFrequency(arr, n, 1, 6, 2));
// Print frequency of 8
// from position 4 to 9
Console.Write("Frequency of 8 from 4 to 9 = " +
findFrequency(arr, n, 4, 9, 8));
}
}
// This code is contributed by Nitin Mittal.
PHP
Javascript
C++
// C++ program to find total count of an element
#include
using namespace std;
unordered_map< int, vector > store;
// Returns frequency of element in arr[left-1..right-1]
int findFrequency(int arr[], int n, int left,
int right, int element)
{
// Find the position of first occurrence of element
int a = lower_bound(store[element].begin(),
store[element].end(),
left)
- store[element].begin();
// Find the position of last occurrence of element
int b = upper_bound(store[element].begin(),
store[element].end(),
right)
- store[element].begin();
return b-a;
}
// Driver code
int main()
{
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
int n = sizeof(arr) / sizeof(arr[0]);
// Storing the indexes of an element in the map
for (int i=0; i
Python3
# Python3 program to find total count of an element
from collections import defaultdict as dict
from bisect import bisect_left as lower_bound
from bisect import bisect_right as upper_bound
store = dict(list)
# Returns frequency of element
# in arr[left-1..right-1]
def findFrequency(arr, n, left, right, element):
# Find the position of
# first occurrence of element
a = lower_bound(store[element], left)
# Find the position of
# last occurrence of element
b = upper_bound(store[element], right)
return b - a
# Driver code
arr = [2, 8, 6, 9, 8, 6, 8, 2, 11]
n = len(arr)
# Storing the indexes of
# an element in the map
for i in range(n):
store[arr[i]].append(i + 1)
# Prfrequency of 2 from position 1 to 6
print("Frequency of 2 from 1 to 6 = ",
findFrequency(arr, n, 1, 6, 2))
# Prfrequency of 8 from position 4 to 9
print("Frequency of 8 from 4 to 9 = ",
findFrequency(arr, n, 4, 9, 8))
# This code is contributed by Mohit Kumar
输出:
Frequency of 2 from 1 to 6 = 1
Frequency of 8 from 4 to 9 = 2
这种方法的时间复杂度是 O(right – left + 1) 或 O(n)
辅助空间:O(1)
一种有效的方法是使用散列。在 C++ 中,我们可以使用 unordered_map
- 首先,我们将每个不同元素在 map[] 中的位置存储为这样的向量
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
map[2] = {1, 8}
map[8] = {2, 5, 7}
map[6] = {3, 6}
ans so on...
- 正如我们所见,map[] 中的元素已经按顺序排列(因为我们从左到右插入了元素),答案归结为使用二分搜索之类的方法找到该哈希 map[] 中的总数。
- 在 C++ 中,我们可以使用lower_bound,它将返回一个迭代器,该迭代器指向范围 [first, last] 中的第一个元素,该元素的值不小于 ‘left’。并且 upper_bound 返回一个指向范围 [first,last) 中第一个元素的迭代器,该元素的值大于“right”。
- 之后,我们只需要减去 upper_bound() 和 lower_bound() 结果即可得到最终答案。例如,假设我们要在[1到6]范围内找到8的总数,那么lower_bound()函数的map[8]将返回结果0(指向2),而upper_bound()将返回结果返回 2(指向 7),所以我们需要减去两个结果,如 2 – 0 = 2 。
以下是上述方法的代码
C++
// C++ program to find total count of an element
#include
using namespace std;
unordered_map< int, vector > store;
// Returns frequency of element in arr[left-1..right-1]
int findFrequency(int arr[], int n, int left,
int right, int element)
{
// Find the position of first occurrence of element
int a = lower_bound(store[element].begin(),
store[element].end(),
left)
- store[element].begin();
// Find the position of last occurrence of element
int b = upper_bound(store[element].begin(),
store[element].end(),
right)
- store[element].begin();
return b-a;
}
// Driver code
int main()
{
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
int n = sizeof(arr) / sizeof(arr[0]);
// Storing the indexes of an element in the map
for (int i=0; i
蟒蛇3
# Python3 program to find total count of an element
from collections import defaultdict as dict
from bisect import bisect_left as lower_bound
from bisect import bisect_right as upper_bound
store = dict(list)
# Returns frequency of element
# in arr[left-1..right-1]
def findFrequency(arr, n, left, right, element):
# Find the position of
# first occurrence of element
a = lower_bound(store[element], left)
# Find the position of
# last occurrence of element
b = upper_bound(store[element], right)
return b - a
# Driver code
arr = [2, 8, 6, 9, 8, 6, 8, 2, 11]
n = len(arr)
# Storing the indexes of
# an element in the map
for i in range(n):
store[arr[i]].append(i + 1)
# Prfrequency of 2 from position 1 to 6
print("Frequency of 2 from 1 to 6 = ",
findFrequency(arr, n, 1, 6, 2))
# Prfrequency of 8 from position 4 to 9
print("Frequency of 8 from 4 to 9 = ",
findFrequency(arr, n, 4, 9, 8))
# This code is contributed by Mohit Kumar
输出:
Frequency of 2 from 1 to 6 = 1
Frequency of 8 from 4 to 9 = 2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。