对于从 1 到 N 的每个 X 值,以 X 作为最频繁元素的子数组的计数
给定一个大小为 N 的数组arr[] ,(其中0 ,对于所有0<=i
例子:
Input: arr[]={2, 1, 2, 3}, N=4
Output:
4 5 1 0
Explanation:
- For X=1, the subarrays where X is the most frequent element, are {2, 1}, {1}, {1, 2}, {1, 2, 3}
- For X=2, the subarrays where X is the most frequent element, are {2}, {2, 1, 2}, {2, 1, 2, 3}, {2}, {2, 3}
- For X=3, the subarray where X is the most frequent element, is {3}
- For X=4, there are no subarrays containing 4.
Input: arr[]={3, 1, 5, 1, 3}, N=5
Output:
12 0 2 0 1
方法:该方法是使用两个循环跟踪每个子数组中最频繁出现的元素,并将它们存储在单独的数组中。请按照以下步骤解决问题:
- 将大小为N的数组ans初始化为0 ,以存储最终答案。
- 从0迭代到N-1 ,对于每个当前索引i ,执行以下操作:
- 将大小为N的频率数组计数初始化为0 ,以存储从1到N的每个元素的当前频率。
- 将一个变量最好初始化为0 ,以存储当前子数组中出现频率最高的元素。
- 从i迭代到N-1 ,对于每个当前索引j ,执行以下操作:
- 增加当前元素的计数即count[arr[j]-1]=count[arr[j]-1]+1
- 检查当前元素的频率,即arr[j]是否大于best 的频率。
- 检查当前元素的频率,即arr[j]是否等于best的频率并且当前元素小于best 。
- 如果其中任何一个为真,则将 best 更新为当前元素,即best=arr[j]
- 增加ans数组的最佳索引,即ans[best-1]=ans[best-1]+1 。
- 打印数组ans 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the number of subarrays where
// X(1<=X<=N) is the most frequent element
int mostFrequent(int arr[], int N)
{
// array to store the final answers
int ans[N] = { 0 };
for (int i = 0; i < N; i++) {
// Array to store current frequencies
int count[N];
// Initialise count
memset(count, 0, sizeof(count));
// Variable to store the
// current most frequent element
int best = 0;
for (int j = i; j < N; j++) {
// Update frequency array
count[arr[j] - 1]++;
if (count[arr[j] - 1] > count[best - 1]
|| (count[arr[j] - 1] == count[best - 1]
&& arr[j] < best)) {
best = arr[j];
}
// Update answer
ans[best - 1]++;
}
}
// Print answer
for (int i = 0; i < N; i++)
cout << ans[i] << " ";
}
// Driver code
int main()
{
// Input
int arr[] = { 2, 1, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
mostFrequent(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to calculate the number of subarrays where
// X(1<=X<=N) is the most frequent element
public static void mostFrequent(int[] arr, int N)
{
// array to store the final answers
int[] ans = new int[N];
for (int i = 0; i < N; i++) {
// Array to store current frequencies
int[] count = new int[N];
// Variable to store the
// current most frequent element
int best = 1;
for (int j = i; j < N; j++) {
// Update frequency array
count[arr[j] - 1]++;
if (best > 0) {
if ((count[arr[j] - 1]
> count[best - 1])
|| (count[arr[j] - 1]
== count[best - 1]
&& arr[j] < best)) {
best = arr[j];
}
// Update answer
ans[best - 1]++;
}
}
}
// Print answer
for (int i = 0; i < N; i++)
System.out.print(ans[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
// Input
int[] arr = { 2, 1, 2, 3 };
int N = arr.length;
// Function call
mostFrequent(arr, N);
}
}
// This code is contributed by sanjoy_62.
Python3
# Python program for the above approach
# Function to calculate the number of subarrays where
# X(1<=X<=N) is the most frequent element
def mostFrequent(arr, N):
# array to store the final answers
ans = [0]*N
for i in range(N):
# Array to store current frequencies
count = [0]*N
# Initialise count
# memset(count, 0, sizeof(count))
# Variable to store the
# current most frequent element
best = 0
for j in range(i,N):
# Update frequency array
count[arr[j] - 1]+=1
if (count[arr[j] - 1] > count[best - 1]
or (count[arr[j] - 1] == count[best - 1]
and arr[j] < best)):
best = arr[j]
# Update answer
ans[best - 1] += 1
# Pranswer
print(*ans)
# Driver code
if __name__ == '__main__':
# Input
arr= [2, 1, 2, 3]
N = len(arr)
# Function call
mostFrequent(arr, N)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to calculate the number of subarrays where
// X(1<=X<=N) is the most frequent element
public static void mostFrequent(int[] arr, int N)
{
// array to store the final answers
int[] ans = new int[N];
for (int i = 0; i < N; i++) {
// Array to store current frequencies
int[] count = new int[N];
// Variable to store the
// current most frequent element
int best = 1;
for (int j = i; j < N; j++) {
// Update frequency array
count[arr[j] - 1]++;
if (best > 0) {
if ((count[arr[j] - 1]
> count[best - 1])
|| (count[arr[j] - 1]
== count[best - 1]
&& arr[j] < best)) {
best = arr[j];
}
// Update answer
ans[best - 1]++;
}
}
}
// Print answer
for (int i = 0; i < N; i++)
Console.Write(ans[i] + " ");
}
// Driver code
public static void Main()
{
// Input
int[] arr = { 2, 1, 2, 3 };
int N = arr.Length;
// Function call
mostFrequent(arr, N);
}
}
// This code is contributed by subham348.
Javascript
输出:
4 5 1 0
时间复杂度: O(N 2 )
辅助空间: O(N)