📌  相关文章
📜  对于从 1 到 N 的每个 X 值,以 X 作为最频繁元素的子数组的计数

📅  最后修改于: 2022-05-13 01:56:06.209000             🧑  作者: Mango

对于从 1 到 N 的每个 X 值,以 X 作为最频繁元素的子数组的计数

给定一个大小为 N 的数组arr[] ,(其中0 ,对于所有0<=i ),任务是计算从1N的每个数字X ,子数组的数量其中X是出现频率最高的元素。在子数组中,一个以上的元素具有最大频率,最小的元素应该被认为是最频繁的。

例子:

方法:该方法是使用两个循环跟踪每个子数组中最频繁出现的元素,并将它们存储在单独的数组中。请按照以下步骤解决问题:

  1. 将大小为N的数组ans初始化为0 ,以存储最终答案。
  2. 0迭代到N-1 ,对于每个当前索引i ,执行以下操作:
    1. 将大小为N的频率数组计数初始化为0 ,以存储从1N的每个元素的当前频率。
    2. 将一个变量最好初始化为0 ,以存储当前子数组中出现频率最高的元素。
    3. i迭代到N-1 ,对于每个当前索引j ,执行以下操作:
      1. 增加当前元素的计数count[arr[j]-1]=count[arr[j]-1]+1
      2. 检查当前元素的频率,即arr[j]是否大于best 的频率。
      3. 检查当前元素的频率,即arr[j]是否等于best的频率并且当前元素小于best
      4. 如果其中任何一个为真,则将 best 更新为当前元素,即best=arr[j]
      5. 增加ans数组的最佳索引,即ans[best-1]=ans[best-1]+1
  3. 打印数组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)