📌  相关文章
📜  从给定范围中删除元素后查找最大数组元素的查询

📅  最后修改于: 2021-09-06 06:35:56             🧑  作者: Mango

给定一个数组arr[]和一个由{L, R}形式的查询组成的数组Q[][] ,每个查询的任务是从索引范围[L ,R] 。如果从给定的索引范围中删除元素后数组变为空,则打印0

例子:

朴素的方法:最简单的方法是在每次查询中删除给定范围内的数组元素后遍历数组以找到最大元素。

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

高效方法:为了优化上述方法,思路是使用两个辅助数组:一个存储前缀最大值(范围[0, i]中的最大元素),另一个数组存储后缀最大值(最大元素在范围内)范围[i, N – 1] )。然后对于每个查询[L, R] ,答案将是prefixMax[l – 1]suffixMax[r + 1]的最大值。请按照以下步骤解决问题:

  • 初始化两个辅助数组:大小为N + 1 的prefixMaxsuffixMax并将prefixMax[0]初始化为arr[0]并将suffixMax[N – 1]初始化为arr[N – 1]
  • 使用变量i迭代范围[1, N – 1]并将prefixMax[i]设置为prefixMax[i – 1]arr[i]的最大值。
  • 使用变量i迭代范围[N – 2, 0]并将suffixMax[i]设置为suffixMax[i + 1]arr[i]的最大值。
  • 遍历数组Q[][]并对每个查询,检查以下条件:
    • 如果L = 0R = N – 1 ,则打印0作为结果。
    • 否则,如果L = 0 ,则打印suffixMax[R + 1]作为结果。
    • 否则,如果R = N – 1 ,则打印prefixMax[L – 1]作为结果。
    • 对于所有其他情况,打印prefixMax[L – 1]suffixMax[R + 1]的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


输出:
15
15
5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live