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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum element
// after removing elements in range [l, r]
int findMaximum(int arr[], int N, int Q,
int queries[][2])
{
// Store prefix maximum element
int prefix_max[N + 1] = { 0 };
// Store suffix maximum element
int suffix_max[N + 1] = { 0 };
// Prefix max till first index
// is first index itself
prefix_max[0] = arr[0];
// Traverse the array to fill
// prefix max array
for (int i = 1; i < N; i++) {
// Store maximum till index i
prefix_max[i]
= max(prefix_max[i - 1],
arr[i]);
}
// Suffix max till last index
// is last index itself
suffix_max[N - 1] = arr[N - 1];
// Traverse the array to fill
// suffix max array
for (int i = N - 2; i >= 0; i--) {
// Store maximum till index i
suffix_max[i]
= max(suffix_max[i + 1],
arr[i]);
}
// Traverse all queries
for (int i = 0; i < Q; i++) {
// Store the starting and the
// ending index of the query
int l = queries[i][0];
int r = queries[i][1];
// Edge Cases
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";
// Otherwise
else
cout << max(prefix_max[l - 1],
suffix_max[r + 1])
<< "\n";
}
}
// Driver Code
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
// Java program for the above approach
class GFG
{
// Function to find the maximum element
// after removing elements in range [l, r]
static void findMaximum(int arr[], int N, int Q,
int queries[][])
{
// Store prefix maximum element
int prefix_max[] = new int[N + 1];
// Store suffix maximum element
int suffix_max[] = new int[N + 1];
// Prefix max till first index
// is first index itself
prefix_max[0] = arr[0];
// Traverse the array to fill
// prefix max array
for (int i = 1; i < N; i++) {
// Store maximum till index i
prefix_max[i]
= Math.max(prefix_max[i - 1],
arr[i]);
}
// Suffix max till last index
// is last index itself
suffix_max[N - 1] = arr[N - 1];
// Traverse the array to fill
// suffix max array
for (int i = N - 2; i >= 0; i--) {
// Store maximum till index i
suffix_max[i]
= Math.max(suffix_max[i + 1],
arr[i]);
}
// Traverse all queries
for (int i = 0; i < Q; i++) {
// Store the starting and the
// ending index of the query
int l = queries[i][0];
int r = queries[i][1];
// Edge Cases
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");
// Otherwise
else
System.out.print(Math.max(prefix_max[l - 1],
suffix_max[r + 1])
+ "\n");
}
}
// Driver Code
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);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to find the maximum element
# after removing elements in range [l, r]
def findMaximum(arr, N, Q, queries):
# Store prefix maximum element
prefix_max = [0]*(N + 1)
# Store suffix maximum element
suffix_max = [0]*(N + 1)
# Prefix max till first index
# is first index itself
prefix_max[0] = arr[0]
# Traverse the array to fill
# prefix max array
for i in range(1, N):
# Store maximum till index i
prefix_max[i]= max(prefix_max[i - 1], arr[i])
# Suffix max till last index
# is last index itself
suffix_max[N - 1] = arr[N - 1]
# Traverse the array to fill
# suffix max array
for i in range(N - 2, -1, -1):
# Store maximum till index i
suffix_max[i] = max(suffix_max[i + 1], arr[i])
# Traverse all queries
for i in range(Q):
# Store the starting and the
# ending index of the query
l = queries[i][0]
r = queries[i][1]
# Edge Cases
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])
# Otherwise
else:
print(max(prefix_max[l - 1], suffix_max[r + 1]))
# Driver Code
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)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the maximum element
// after removing elements in range [l, r]
static void findMaximum(int[] arr, int N, int Q,
int[,] queries)
{
// Store prefix maximum element
int[] prefix_max = new int[N + 1];
// Store suffix maximum element
int[] suffix_max = new int[N + 1];
// Prefix max till first index
// is first index itself
prefix_max[0] = arr[0];
// Traverse the array to fill
// prefix max array
for (int i = 1; i < N; i++)
{
// Store maximum till index i
prefix_max[i]
= Math.Max(prefix_max[i - 1],
arr[i]);
}
// Suffix max till last index
// is last index itself
suffix_max[N - 1] = arr[N - 1];
// Traverse the array to fill
// suffix max array
for (int i = N - 2; i >= 0; i--)
{
// Store maximum till index i
suffix_max[i]
= Math.Max(suffix_max[i + 1],
arr[i]);
}
// Traverse all queries
for (int i = 0; i < Q; i++)
{
// Store the starting and the
// ending index of the query
int l = queries[i, 0];
int r = queries[i, 1];
// Edge Cases
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");
// Otherwise
else
Console.Write(Math.Max(prefix_max[l - 1],
suffix_max[r + 1])
+ "\n");
}
}
// Driver Code
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);
}
}
// This code is contributed by sanjoy_62.
JavaScript
<script>
// JavaScript program of the above approach
// Function to find the maximum element
// after removing elements in range [l, r]
function findMaximum(arr, N, Q,
queries)
{
// Store prefix maximum element
let prefix_max = [];
// Store suffix maximum element
let suffix_max = [];
// Prefix max till first index
// is first index itself
prefix_max[0] = arr[0];
// Traverse the array to fill
// prefix max array
for (let i = 1; i < N; i++) {
// Store maximum till index i
prefix_max[i]
= Math.max(prefix_max[i - 1],
arr[i]);
}
// Suffix max till last index
// is last index itself
suffix_max[N - 1] = arr[N - 1];
// Traverse the array to fill
// suffix max array
for (let i = N - 2; i >= 0; i--) {
// Store maximum till index i
suffix_max[i]
= Math.max(suffix_max[i + 1],
arr[i]);
}
// Traverse all queries
for (let i = 0; i < Q; i++)
{
// Store the starting and the
// ending index of the query
let l = queries[i][0];
let r = queries[i][1];
// Edge Cases
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/>");
// Otherwise
else
document.write(Math.max(prefix_max[l - 1],
suffix_max[r + 1])
+ "<br/>");
}
}
// Driver Code
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);
// This code is contributed by avijitmondal1998.
</script>
Time Complexity: O(N + Q)
Auxiliary Space: O(N)
Similar Reads
Queries to find the maximum and minimum array elements excluding elements from a given range
Given an array arr[] consisting of N integers and an array Q[][] consisting of queries of the form [L, R]., the task for each query is to find the maximum and minimum array elements in the array excluding the elements from the given range. Examples: Input: arr[] = {2, 3, 1, 8, 3, 5, 7, 4}, Q[][] = {
15+ min read
Find maximum element among the elements with minimum frequency in given Array
Given an array arr[] consisting of N integers, the task is to find the maximum element with the minimum frequency. Examples: Input: arr[] = {2, 2, 5, 50, 1}Output: 50Explanation:The element with minimum frequency is {1, 5, 50}. The maximum element among these element is 50. Input: arr[] = {3, 2, 5,
13 min read
Minimum element left from the array after performing given operations
Given an array arr[] of N integers, the task is to remove the elements from both the ends of the array i.e. in a single operation, either the first or the last element can be removed from the current remaining elements of the array. This operation needs to be performed in such a way that the last el
3 min read
Range Queries to find the Element having Maximum Digit Sum
Given an array Arr of N integers and Q queries, each query having a range from L to R. Find the element having maximum digit sum for the range L to R, and if more than one element has a maximum digit sum, then find the maximum element out of those. Examples: Input: Arr[] = { 16, 12, 43, 55} Q = 2 L
15+ min read
Find final Array after subtracting maximum from all elements K times
Given an array arr[] of N integers and an integer K, the task is to do the below operations with this array K times. Operations are as follows: Find the maximum element (say M) from the array.Now replace every element with M-arri. where 1 ⤠i ⤠N. Examples: Input: N = 6, K = 3, arr[] = {5, 38, 4, 96
9 min read
Find maximum value of the last element after reducing the array with given operations
Given an array arr[] of N elements, you have to perform the following operation on the given array until the array is reduced to a single elements, Choose two indices i and j such that i != j.Replace arr[i] with arr[i] - arr[j] and remove arr[j] from the array. The task is to maximize and print the
7 min read
Maximum XOR Queries With an Element From Array
Given an array arr[] of size n containing non-negative integers and also given a list of q queries in a 2D array queries[][], where each query is of the form [xi, mi]. For each query, you need to find the maximum value of (xi XOR arr[j]) such that arr[j] is less than or equal to mi.In simple terms,
15+ min read
Maximum element present in the array after performing queries to add K to range of indices [L, R]
Given an array arr[] consisting of N integers, ( initially set to 0 ) and an array Q[], consisting of queries of the form {l, r, k}, the task for each query is to add K to the indices l to r(both inclusive). After performing all queries, return the maximum element present the array. Example: Input:
7 min read
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements
Given an array arr[] consisting of N integers, the task is to print the last remaining array element after repeatedly selecting pairs of increasing adjacent elements (arr[i], arr[i + 1]) and removing any of the elements in the pair. If it is not possible to reduce the array to a single element, then
4 min read
Maximum length of Strictly Increasing Sub-array after removing at most one element
Given an array arr[], the task is to remove at most one element and calculate the maximum length of strictly increasing subarray. Examples: Input: arr[] = {1, 2, 5, 3, 4} Output: 4 After deleting 5, the resulting array will be {1, 2, 3, 4} and the maximum length of its strictly increasing subarray i
10 min read