Queries to count occurrences of maximum array element in subarrays starting from given indices
Last Updated :
27 Jan, 2023
Given two arrays arr[] and q[] consisting of N integers, the task is for each query q[i] is to determine the number of times the maximum array element occurs in the subarray [q[i], arr[N – 1]].
Examples:
Input: arr[] = {5, 4, 5, 3, 2}, q[] = {1, 2, 3, 4, 5}
Output: 2 1 1 1 1
Explanation:
For the first query index start from 1. The subarray is [5, 4, 5, 3, 2], the maximum value is 5 and it’s occurrence in subarray is 2.
For the second query index start from 2. The subarray is [4, 5, 3, 2], the maximum value is 5 and it’s occurrence in subarray is 1.
For the third query index start from 3. The subarray is [5, 3, 2], the maximum value is 5 and it’s occurrence in subarray is 1.
For the forth query index start from 4. The subarray is [3, 2], the maximum value is 3 and it’s occurrence in subarray is 1.
For the fifth query index start from 5. The subarray is [2], the maximum value is 2 and it’s occurrence in subarray is 1.
Input: arr[] = {2, 1, 2}, q[] = {1, 2, 3}
Output: 2 1 1
Naive Approach: The simplest approach is to traverse the array and find the maximum array element. Now, for each query q[i], traverse the subarray [q[i], arr[N – 1]], and print the count of occurrences of the maximum element in the subarray.
Follow the steps below to implement the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void FreqOfMaxElement(vector< int > arr, vector< int > q)
{
for ( int i = 0; i < q.size(); i++) {
int start = q[i] - 1;
int count = 0;
int maxx
= *max_element(arr.begin() + start, arr.end());
for ( int j = start; j < arr.size(); j++) {
if (arr[j] == maxx) {
count++;
}
}
cout << count << " " ;
}
}
int main()
{
vector< int > arr = { 5, 4, 5, 3, 2 };
vector< int > q = { 1, 2, 3, 4, 5 };
FreqOfMaxElement(arr, q);
}
|
Java
import java.util.*;
import java.io.*;
public class Gfg {
public static void FreqOfMaxElement(List<Integer> arr,
List<Integer> q)
{
for ( int i = 0 ; i < q.size(); i++) {
int start = q.get(i) - 1 ;
int count = 0 ;
int maxx = Collections.max(arr.subList(start, arr.size()));
for ( int j = start; j < arr.size(); j++) {
if (arr.get(j) == maxx) {
count++;
}
}
System.out.print(count + " " );
}
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList( 5 , 4 , 5 , 3 , 2 );
List<Integer> q = Arrays.asList( 1 , 2 , 3 , 4 , 5 );
FreqOfMaxElement(arr, q);
}
}
|
Python3
from collections import Counter
def FreqOfMaxElement(arr, q):
for i in range ( len (q)):
start = q[i] - 1
count = 0
maxx = max (arr[start:])
for j in range (start, len (arr)):
if arr[j] = = maxx:
count + = 1
print (count, end = " " )
if __name__ = = "__main__" :
arr = [ 5 , 4 , 5 , 3 , 2 ]
q = [ 1 , 2 , 3 , 4 , 5 ]
FreqOfMaxElement(arr, q)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static void FreqOfMaxElement( int [] arr, int [] q)
{
for ( int i = 0; i < q.Length; i++) {
int start = q[i] - 1;
int count = 0;
int maxx=-1;
for ( int j=start; j<arr.Length; j++)
maxx=Math.Max(maxx, arr[j]);
for ( int j = start; j < arr.Length; j++) {
if (arr[j] == maxx) {
count++;
}
}
Console.Write(count + " " );
}
}
public static void Main ( string [] args)
{
int [] arr = { 5, 4, 5, 3, 2 };
int [] q = { 1, 2, 3, 4, 5 };
FreqOfMaxElement(arr, q);
}
}
|
Javascript
function FreqOfMaxElement(arr, q)
{
for (let i = 0; i < q.length; i++) {
let start = q[i] - 1;
let count = 0;
let maxx=Number.MIN_SAFE_INTEGER;
for (let i=start; i<arr.length; i++)
{
maxx=Math.max(arr[i], maxx);
}
for (let j = start; j < arr.length; j++) {
if (arr[j] == maxx) {
count++;
}
}
document.write(count);
document.write( " " );
}
}
let arr = [ 5, 4, 5, 3, 2 ];
let q = [ 1, 2, 3, 4, 5 ];
FreqOfMaxElement(arr, q);
|
Time Complexity: O(N*Q), where N and Q are the lengths of the given array and query respectively.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use Hashing. Below are the steps:
- Create an array maxFreqVec[] to store the occurrence of the max element from given index q[i] to N.
- Traverse the array arr[] from the right and keep track of the maximum element in the array and update the array maxFreqVec[] at that index with the occurrence of that maximum element.
- After the above steps, traverse over the array q[] and print the value maxFreqVec[q[i] – 1] as the maximum element in each subarray for each query.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void FreqOfMaxElement(vector< int > arr,
vector< int > q)
{
vector< int > maxFreqVec(arr.size());
int maxSoFar = INT_MIN;
int maxFreq = 0;
for ( int i = arr.size() - 1; i >= 0; i--)
{
if (arr[i] > maxSoFar)
{
maxSoFar = arr[i];
maxFreq = 1;
}
else if (arr[i] == maxSoFar)
{
maxFreq++;
}
maxFreqVec[i] = maxFreq;
}
for ( int k : q)
{
cout << maxFreqVec[k - 1] << " " ;
}
}
int main()
{
vector< int > arr = { 5, 4, 5, 3, 2 };
vector< int > q = { 1, 2, 3, 4, 5 };
FreqOfMaxElement(arr, q);
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static void FreqOfMaxElement(
int [] arr, int [] q)
{
int [] maxFreqVec
= new int [arr.length];
int maxSoFar = Integer.MIN_VALUE;
int maxFreq = 0 ;
for ( int i = arr.length - 1 ;
i >= 0 ; i--) {
if (arr[i] > maxSoFar) {
maxSoFar = arr[i];
maxFreq = 1 ;
}
else if (arr[i] == maxSoFar) {
maxFreq++;
}
maxFreqVec[i] = maxFreq;
}
for ( int k : q) {
System.out.print(
maxFreqVec[k - 1 ] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 5 , 4 , 5 , 3 , 2 };
int [] q = { 1 , 2 , 3 , 4 , 5 };
FreqOfMaxElement(arr, q);
}
}
|
Python3
import sys;
def FreqOfMaxElement(arr, q):
maxFreqVec = [ 0 ] * ( len (arr));
maxSoFar = - sys.maxsize;
maxFreq = 0 ;
for i in range ( len (arr) - 1 ,
- 1 , - 1 ):
if (arr[i] > maxSoFar):
maxSoFar = arr[i];
maxFreq = 1 ;
elif (arr[i] = = maxSoFar):
maxFreq + = 1 ;
maxFreqVec[i] = maxFreq;
for i in range ( 0 , len (q)):
print (maxFreqVec[q[i] - 1 ],
end = " " );
if __name__ = = '__main__' :
arr = [ 5 , 4 , 5 , 3 , 2 ];
q = [ 1 , 2 , 3 , 4 , 5 ];
FreqOfMaxElement(arr, q);
|
C#
using System;
class GFG{
static void FreqOfMaxElement( int [] arr,
int [] q)
{
int [] maxFreqVec = new int [arr.Length];
int maxSoFar = Int32.MinValue;
int maxFreq = 0;
for ( int i = arr.Length - 1;
i >= 0; i--)
{
if (arr[i] > maxSoFar)
{
maxSoFar = arr[i];
maxFreq = 1;
}
else if (arr[i] == maxSoFar)
{
maxFreq++;
}
maxFreqVec[i] = maxFreq;
}
foreach ( int k in q)
{
Console.Write(maxFreqVec[k - 1] +
" " );
}
}
static void Main()
{
int [] arr = {5, 4, 5, 3, 2};
int [] q = {1, 2, 3, 4, 5};
FreqOfMaxElement(arr, q);
}
}
|
Javascript
<script>
function FreqOfMaxElement(arr, q)
{
var maxFreqVec = new Array(arr.length);
var maxSoFar = -1000000000;
var maxFreq = 0;
for ( var i = arr.length - 1; i >= 0; i--)
{
if (arr[i] > maxSoFar)
{
maxSoFar = arr[i];
maxFreq = 1;
}
else if (arr[i] == maxSoFar)
{
maxFreq++;
}
maxFreqVec[i] = maxFreq;
}
q.forEach(k => {
document.write( maxFreqVec[k - 1] + " " );
});
}
var arr = [ 5, 4, 5, 3, 2 ];
var q = [ 1, 2, 3, 4, 5 ];
FreqOfMaxElement(arr, q);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...