📌  相关文章
📜  从给定索引开始计算子数组中最大数组元素的出现次数的查询

📅  最后修改于: 2021-05-18 01:13:57             🧑  作者: Mango

给定由N个整数组成的两个数组arr []q [] ,任务是针对每个查询q [i]确定子数组[q [i]中最大数组元素出现的次数arr [N – 1]]

例子:

天真的方法:最简单的方法是遍历数组并找到最大的数组元素。现在,对于每个查询q [i],遍历子数组[q [i],arr [N – 1]]并打印子数组中最大元素的出现次数。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:为了优化上述方法,我们的想法是使用哈希。步骤如下:

  • 创建一个数组maxFreqVec []来存储从给定索引q [i]N的max元素的出现。
  • 从右侧遍历数组arr []并跟踪该数组中的最大元素,并在出现该最大元素时在该索引处更新该数组maxFreqVec []
  • 完成上述步骤后,遍历数组q []并输出值maxFreqVec [q [i] – 1]作为每个查询的每个子数组中的最大元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to find occurrence of
// max element in given subarray
void FreqOfMaxElement(vector arr,
                      vector q)
{
     
    // Store the frequency of maximum
    // element
    vector maxFreqVec(arr.size());
 
    int maxSoFar = INT_MIN;
    int maxFreq = 0;
 
    // Traverse over the array arr[]
    // from right to left
    for(int i = arr.size() - 1; i >= 0; i--)
    {
         
        // If curr element is greater
        // than maxSofar
        if (arr[i] > maxSoFar)
        {
             
            // Reset maxSofar and maxFreq
            maxSoFar = arr[i];
            maxFreq = 1;
        }
 
        // If curr is equal to maxSofar
        else if (arr[i] == maxSoFar)
        {
             
            // Increment the maxFreq
            maxFreq++;
        }
 
        // Update maxFreqVec[i]
        maxFreqVec[i] = maxFreq;
    }
 
    // Print occurrence of maximum
    // element for each query
    for(int k : q)
    {
        cout << maxFreqVec[k - 1] << " ";
    }
}
 
// Driver Code
int main()
{
    vector arr = { 5, 4, 5, 3, 2 };
    vector q = { 1, 2, 3, 4, 5 };
     
    // Function Call
    FreqOfMaxElement(arr, q);
}
 
// This code is contributed by mohit kumar 29


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to find occurrence of
    // max element in given subarray
    static void FreqOfMaxElement(
        int[] arr, int[] q)
    {
 
        // Store the frequency of maximum
        // element
        int[] maxFreqVec
            = new int[arr.length];
 
        int maxSoFar = Integer.MIN_VALUE;
        int maxFreq = 0;
 
        // Traverse over the array arr[]
        // from right to left
        for (int i = arr.length - 1;
             i >= 0; i--) {
 
            // If curr element is greater
            // than maxSofar
            if (arr[i] > maxSoFar) {
 
                // Reset maxSofar and maxFreq
                maxSoFar = arr[i];
                maxFreq = 1;
            }
 
            // If curr is equal to maxSofar
            else if (arr[i] == maxSoFar) {
 
                // Increment the maxFreq
                maxFreq++;
            }
 
            // Update maxFreqVec[i]
            maxFreqVec[i] = maxFreq;
        }
 
        // Print occurrence of maximum
        // element for each query
        for (int k : q) {
            System.out.print(
                maxFreqVec[k - 1] + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 5, 4, 5, 3, 2 };
        int[] q = { 1, 2, 3, 4, 5 };
 
        // Function Call
        FreqOfMaxElement(arr, q);
    }
}


Python3
# Python3 program for
# the above approach
import sys;
 
# Function to find occurrence
# of max element in given
# subarray
def FreqOfMaxElement(arr, q):
   
    # Store the frequency of
    # maximum element
    maxFreqVec = [0] * (len(arr));
 
    maxSoFar = -sys.maxsize;
    maxFreq = 0;
 
    # Traverse over the array
    # arr from right to left
    for i in range(len(arr)-1,
                   -1, -1):
 
        # If curr element is
        # greater than maxSofar
        if (arr[i] > maxSoFar):
 
            # Reset maxSofar
            # and maxFreq
            maxSoFar = arr[i];
            maxFreq = 1;
 
        # If curr is equal to
        # maxSofar
        elif (arr[i] == maxSoFar):
 
            # Increment the
            # maxFreq
            maxFreq += 1;
 
        # Update maxFreqVec[i]
        maxFreqVec[i] = maxFreq;
 
    # Proccurrence of maximum
    # element for each query
    for i in range(0, len(q)):
        print(maxFreqVec[q[i] - 1],
              end = " ");
 
# Driver Code
if __name__ == '__main__':
   
    arr = [5, 4, 5, 3, 2];
    q = [1, 2, 3, 4, 5];
 
    # Function Call
    FreqOfMaxElement(arr, q);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the
// above approach
using System;
class GFG{
     
// Function to find occurrence of
// max element in given subarray
static void FreqOfMaxElement(int[] arr,
                             int[] q)
{
  // Store the frequency of
  // maximum element
  int[] maxFreqVec = new int[arr.Length];
 
  int maxSoFar = Int32.MinValue;
  int maxFreq = 0;
 
  // Traverse over the array arr[]
  // from right to left
  for (int i = arr.Length - 1;
           i >= 0; i--)
  {
    // If curr element is greater
    // than maxSofar
    if (arr[i] > maxSoFar)
    {
      // Reset maxSofar and maxFreq
      maxSoFar = arr[i];
      maxFreq = 1;
    }
 
    // If curr is equal to maxSofar
    else if (arr[i] == maxSoFar)
    {
      // Increment the maxFreq
      maxFreq++;
    }
 
    // Update maxFreqVec[i]
    maxFreqVec[i] = maxFreq;
  }
 
  // Print occurrence of maximum
  // element for each query
  foreach (int k in q)
  {
    Console.Write(maxFreqVec[k - 1] +
                  " ");
  }
}
     
// Driver code
static void Main()
{
  int[] arr = {5, 4, 5, 3, 2};
  int[] q = {1, 2, 3, 4, 5};
 
  // Function Call
  FreqOfMaxElement(arr, q);
  }
}
 
// This code is contributed by divyeshrabadiya07


输出:
2 1 1 1 1









时间复杂度: O(N)
辅助空间: O(N)