Queries to find the maximum array element after removing elements from a given range
Last Updated :
30 Jun, 2021
Given an array arr[] and an array Q[][] consisting of queries of the form of {L, R}, the task for each query is to find the maximum array element after removing array elements from the range of indices [L, R]. If the array becomes empty after removing the elements from given range of indices, then print 0.
Examples:
Input: arr[] = {5, 6, 8, 10, 15}, Q = {{0, 1}, {0, 2}, {1, 4}}
Output:
15
15
5
Explanation:
For the first query {0 1}: Remove array elements in range [0, 1], array modifies to {8, 10, 15}. Hence, the maximum element is 15.
For the second query {0, 2}: Remove array elements in range [0, 2], array modifies to {10, 15}. Hence, the maximum element is 15.
For the third query {1 4}: Remove array elements in range [1, 4], array modifies to {5}. Hence, the maximum element is 5.
Input: arr[] = {8, 12, 14, 10, 13}, Q = {{0, 3}, {0, 4}, {4, 4}}
Output:
13
-1
14
Naive Approach: The simplest approach is to traverse the array to find the maximum element after removing the array elements in the given range in each query.
Time Complexity: O(N*Q)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to use two auxiliary arrays: one will store the prefix maximum (maximum element in the range [0, i]) and the other array will store the suffix maximum (maximum element in the range [i, N – 1]). Then for every query [L, R], the answer will be a maximum of prefixMax[l – 1] and suffixMax[r + 1]. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMaximum( int arr[], int N, int Q,
int queries[][2])
{
int prefix_max[N + 1] = { 0 };
int suffix_max[N + 1] = { 0 };
prefix_max[0] = arr[0];
for ( int i = 1; i < N; i++) {
prefix_max[i]
= max(prefix_max[i - 1],
arr[i]);
}
suffix_max[N - 1] = arr[N - 1];
for ( int i = N - 2; i >= 0; i--) {
suffix_max[i]
= max(suffix_max[i + 1],
arr[i]);
}
for ( int i = 0; i < Q; i++) {
int l = queries[i][0];
int r = queries[i][1];
if (l == 0 && r == (N - 1))
cout << "0\n" ;
else if (l == 0)
cout << suffix_max[r + 1]
<< "\n" ;
else if (r == (N - 1))
cout << prefix_max[l - 1]
<< "\n" ;
else
cout << max(prefix_max[l - 1],
suffix_max[r + 1])
<< "\n" ;
}
}
int main()
{
int arr[] = { 5, 6, 8, 10, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
int queries[][2] = { { 0, 1 }, { 0, 2 }, { 1, 4 } };
int Q = sizeof (queries) / sizeof (queries[0]);
findMaximum(arr, N, Q, queries);
return 0;
}
|
Java
class GFG
{
static void findMaximum( int arr[], int N, int Q,
int queries[][])
{
int prefix_max[] = new int [N + 1 ];
int suffix_max[] = new int [N + 1 ];
prefix_max[ 0 ] = arr[ 0 ];
for ( int i = 1 ; i < N; i++) {
prefix_max[i]
= Math.max(prefix_max[i - 1 ],
arr[i]);
}
suffix_max[N - 1 ] = arr[N - 1 ];
for ( int i = N - 2 ; i >= 0 ; i--) {
suffix_max[i]
= Math.max(suffix_max[i + 1 ],
arr[i]);
}
for ( int i = 0 ; i < Q; i++) {
int l = queries[i][ 0 ];
int r = queries[i][ 1 ];
if (l == 0 && r == (N - 1 ))
System.out.print( "0\n" );
else if (l == 0 )
System.out.print(suffix_max[r + 1 ]
+ "\n" );
else if (r == (N - 1 ))
System.out.print(prefix_max[l - 1 ]
+ "\n" );
else
System.out.print(Math.max(prefix_max[l - 1 ],
suffix_max[r + 1 ])
+ "\n" );
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 6 , 8 , 10 , 15 };
int N = arr.length;
int queries[][] = { { 0 , 1 }, { 0 , 2 }, { 1 , 4 } };
int Q = queries.length;
findMaximum(arr, N, Q, queries);
}
}
|
Python3
def findMaximum(arr, N, Q, queries):
prefix_max = [ 0 ] * (N + 1 )
suffix_max = [ 0 ] * (N + 1 )
prefix_max[ 0 ] = arr[ 0 ]
for i in range ( 1 , N):
prefix_max[i] = max (prefix_max[i - 1 ], arr[i])
suffix_max[N - 1 ] = arr[N - 1 ]
for i in range (N - 2 , - 1 , - 1 ):
suffix_max[i] = max (suffix_max[i + 1 ], arr[i])
for i in range (Q):
l = queries[i][ 0 ]
r = queries[i][ 1 ]
if (l = = 0 and r = = (N - 1 )):
print ( "0" )
elif (l = = 0 ):
print (suffix_max[r + 1 ])
elif (r = = (N - 1 )):
print (prefix_max[l - 1 ])
else :
print ( max (prefix_max[l - 1 ], suffix_max[r + 1 ]))
if __name__ = = '__main__' :
arr = [ 5 , 6 , 8 , 10 , 15 ]
N = len (arr)
queries = [ [ 0 , 1 ], [ 0 , 2 ], [ 1 , 4 ] ]
Q = len (queries)
findMaximum(arr, N, Q, queries)
|
C#
using System;
public class GFG
{
static void findMaximum( int [] arr, int N, int Q,
int [,] queries)
{
int [] prefix_max = new int [N + 1];
int [] suffix_max = new int [N + 1];
prefix_max[0] = arr[0];
for ( int i = 1; i < N; i++)
{
prefix_max[i]
= Math.Max(prefix_max[i - 1],
arr[i]);
}
suffix_max[N - 1] = arr[N - 1];
for ( int i = N - 2; i >= 0; i--)
{
suffix_max[i]
= Math.Max(suffix_max[i + 1],
arr[i]);
}
for ( int i = 0; i < Q; i++)
{
int l = queries[i, 0];
int r = queries[i, 1];
if (l == 0 && r == (N - 1))
Console.Write( "0\n" );
else if (l == 0)
Console.Write(suffix_max[r + 1]
+ "\n" );
else if (r == (N - 1))
Console.Write(prefix_max[l - 1]
+ "\n" );
else
Console.Write(Math.Max(prefix_max[l - 1],
suffix_max[r + 1])
+ "\n" );
}
}
static public void Main ()
{
int [] arr = { 5, 6, 8, 10, 15 };
int N = arr.Length;
int [,] queries = { { 0, 1 }, { 0, 2 }, { 1, 4 } };
int Q = queries.GetLength(0);
findMaximum(arr, N, Q, queries);
}
}
|
Javascript
<script>
function findMaximum(arr, N, Q,
queries)
{
let prefix_max = [];
let suffix_max = [];
prefix_max[0] = arr[0];
for (let i = 1; i < N; i++) {
prefix_max[i]
= Math.max(prefix_max[i - 1],
arr[i]);
}
suffix_max[N - 1] = arr[N - 1];
for (let i = N - 2; i >= 0; i--) {
suffix_max[i]
= Math.max(suffix_max[i + 1],
arr[i]);
}
for (let i = 0; i < Q; i++)
{
let l = queries[i][0];
let r = queries[i][1];
if (l == 0 && r == (N - 1))
document.write( "0" );
else if (l == 0)
document.write(suffix_max[r + 1]
+ "<br/>" );
else if (r == (N - 1))
document.write(prefix_max[l - 1]
+ "<br/>" );
else
document.write(Math.max(prefix_max[l - 1],
suffix_max[r + 1])
+ "<br/>" );
}
}
let arr = [ 5, 6, 8, 10, 15 ];
let N = arr.length;
let queries = [[ 0, 1 ], [ 0, 2 ], [ 1, 4 ]];
let Q = queries.length;
findMaximum(arr, N, Q, queries);
</script>
|
Time Complexity: O(N + Q)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...