给定一个由N 个整数和一个正整数M组成的数组arr[] ,任务是找到每个M长度子数组的最大频率 ( 0 < M ≤ N )。
例子:
Input: arr[] = {1, 2, 3, 1, 2, 4, 1, 4, 4}, M = 4
Output: 2 2 1 2 2 3
Explanation:
All the M length sub-arrays with the maximum frequency of any element are:
- {1, 2, 3, 1}, The maximum frequency of an element is 2.
- {2, 3, 1, 2}, The maximum frequency of an element is 2.
- {3, 1, 2, 4}, The maximum frequency of an element is 1.
- {1, 2, 4, 1}, The maximum frequency of an element is 2.
- {2, 4, 1, 4}, The maximum frequency of an element is 2.
- {4, 1, 4, 4}, The maximum frequency of an element is 3.
Input: arr[] = {1, 1, 2, 2, 3, 5}, M = 4
Output: 2 2 2ing
方法:给定的问题可以通过找到每个M 大小的子阵列的频率来解决,打印所有的最大频率。请按照以下步骤解决给定的问题:
- 初始化一个无序映射,比如M来存储数组元素的频率。
- 初始化变量,假设val为0以存储子数组元素的最大频率。
- 使用变量i在范围[0, N] 上迭代并执行以下步骤:
- 如果(i – M)的值大于等于0 ,则从映射M 中减小A[i – M]的值。
- 在映射M 中添加arr[i]的值。
- 迭代映射M并将val的值更新为val或x.second的最大值。
- 打印val的值作为当前 M 大小子数组的最大值。
下面是上述方法的一个实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the frequency of
// the most common element in each M
// length subarrays
void maxFrequencySubarrayUtil(
vector A, int N, int M)
{
int i = 0;
// Stores frequency of array element
unordered_map m;
// Stores the maximum frequency
int val = 0;
// Iterate for the first sub-array
// and store the maximum
for (; i < M; i++) {
m[A[i]]++;
val = max(val, m[A[i]]);
}
// Print the maximum frequency for
// the first subarray
cout << val << " ";
// Iterate over the range [M, N]
for (i = M; i < N; i++) {
// Subtract the A[i - M] and
// add the A[i] in the map
m[A[i - M]]--;
m[A[i]]++;
val = 0;
// Find the maximum frequency
for (auto x : m) {
val = max(val, x.second);
}
// Print the maximum frequency
// for the current subarray
cout << val << " ";
}
}
// Driver Code
int main()
{
vector A = { 1, 1, 2, 2, 3, 5 };
int N = A.size();
int M = 4;
maxFrequencySubarrayUtil(A, N, M);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the frequency of
// the most common element in each M
// length subarrays
static void maxFrequencySubarrayUtil(int[] A, int N, int M) {
int i = 0;
// Stores frequency of array element
HashMap m = new HashMap();
// Stores the maximum frequency
int val = 0;
// Iterate for the first sub-array
// and store the maximum
for (; i < M; i++) {
if (m.containsKey(A[i])) {
m.put(A[i], m.get(A[i]) + 1);
} else {
m.put(A[i], 1);
}
val = Math.max(val, m.get(A[i]));
}
// Print the maximum frequency for
// the first subarray
System.out.print(val + " ");
// Iterate over the range [M, N]
for (i = M; i < N; i++) {
// Subtract the A[i - M] and
// add the A[i] in the map
if (m.containsKey(i - M)) {
m.put(i - M, m.get(i - M) - 1);
}
if (m.containsKey(A[i])) {
m.put(A[i], m.get(A[i]) + 1);
} else {
m.put(A[i], 1);
}
val = 0;
// Find the maximum frequency
for (Map.Entry x : m.entrySet()) {
val = Math.max(val, x.getValue());
}
// Print the maximum frequency
// for the current subarray
System.out.print(val + " ");
}
}
// Driver Code
public static void main(String[] args) {
int[] A = { 1, 1, 2, 2, 3, 5 };
int N = A.length;
int M = 4;
maxFrequencySubarrayUtil(A, N, M);
}
}
// This code is contributed by 29AjayKumar
输出:
2 2 2
时间复杂度: O(N*M)
辅助空间: O(M)