📌  相关文章
📜  检查给定索引范围内的子数组是否不减少的查询

📅  最后修改于: 2021-09-06 06:38:40             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[]和一个由K个类型为{L, R} 的查询组成的数组Q[][2] ,每个查询的任务是检查子数组{arr[L], ..数组的arr[R]}是否非递减。如果发现是真的,则打印“是” 。否则,打印“”。

例子:

朴素的方法:最简单的方法是在每个查询的索引范围[L, R]上遍历数组并检查子数组是否按升序排序。如果发现是真的,则打印“是” 。否则,打印“”。
时间复杂度: O(N * Q)
辅助空间: O(1)

高效的方法:可以通过预先计算范围[1, i]中满足arr[i] > arr[i + 1]的相邻元素的数量来优化上述方法这导致对范围内此类索引的数量进行恒定时间计算[L, R – 1] 。请按照以下步骤解决问题:

  • 初始化一个数组,比如pre[] ,以存储从起始索引开始的索引计数,相邻元素按递增顺序排列。
  • 迭代范围[1, N – 1]并分配pre[i] = pre[i – 1]然后将pre[i]增加1 ,如果arr[i – 1] > arr[i]
  • 遍历数组Q[][]并对每个查询{L, R} ,如果pre[R – 1] – pre[L – 1]0 ,则打印“Yes” 。否则,打印“”。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to perform queries to check if
// subarrays over a given range of indices
//  is non-decreasing or not
void checkSorted(int arr[], int N,
                 vector >& Q)
{
    // Stores count of indices up to i
    // such that arr[i] > arr[i + 1]
    int pre[N] = { 0 };
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        // Update pre[i]
        pre[i] = pre[i - 1]
                 + (arr[i - 1] > arr[i]);
    }
 
    // Traverse the array Q[][]
    for (int i = 0; i < Q.size(); i++) {
 
        int l = Q[i][0];
        int r = Q[i][1] - 1;
 
        // If pre[r] - pre[l-1] exceeds 0
        if (pre[r] - pre[l - 1] == 0)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 7, 3, 4, 9 };
    vector > Q = { { 1, 2 },
                               { 2, 4 } };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    checkSorted(arr, N, Q);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to perform queries to check if
  // subarrays over a given range of indices
  //  is non-decreasing or not
  static void checkSorted(int[] arr, int N, int[][] Q)
  {
 
    // Stores count of indices up to i
    // such that arr[i] > arr[i + 1]
    int[] pre = new int[N];
 
    // Traverse the array
    for (int i = 1; i < N; i++)
    {
 
      // Update pre[i]
      if((arr[i - 1] > arr[i]))
        pre[i] = pre[i - 1] + 1;
      else
        pre[i] = pre[i - 1];
    }
 
    // Traverse the array Q[][]
    for (int i = 0; i < Q.length; i++)
    {
      int l = Q[i][0];
      int r = Q[i][1] - 1;
 
      // If pre[r] - pre[l-1] exceeds 0
      if (pre[r] - pre[l - 1] == 0)
        System.out.println("Yes");
      else
        System.out.println("No");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 1, 7, 3, 4, 9 };
    int Q[][] = { { 1, 2 }, { 2, 4 } };
 
    int N = arr.length;
 
    // Function Call
    checkSorted(arr, N, Q);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
 
# Function to perform queries to check if
# subarrays over a given range of indices
# is non-decreasing or not
def checkSorted(arr, N, Q):
   
    # Stores count of indices up to i
    # such that arr[i] > arr[i + 1]
    pre = [0]*(N)
 
    # Traverse the array
    for i in range(1, N):
 
        # Update pre[i]
        pre[i] = pre[i - 1] + (arr[i - 1] > arr[i])
 
    # Traverse the array Q[][]
    for i in range(len(Q)):
        l = Q[i][0]
        r = Q[i][1] - 1
 
        # If pre[r] - pre[l-1] exceeds 0
        if (pre[r] - pre[l - 1] == 0):
            print("Yes")
        else:
            print("No")
 
# Driver Code
if __name__ == '__main__':
    arr =[1, 7, 3, 4, 9]
    Q = [ [ 1, 2 ],[ 2, 4 ] ]
    N = len(arr)
 
    # Function Call
    checkSorted(arr, N, Q)
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
 
using System;
 
public class GFG{
     
    // Function to perform queries to check if
    // subarrays over a given range of indices
    // is non-decreasing or not
    static void checkSorted(int[] arr, int N, int[,] Q)
    {
        // Stores count of indices up to i
    // such that arr[i] > arr[i + 1]
    int[] pre = new int[N];
  
    // Traverse the array
    for (int i = 1; i < N; i++)
    {
  
      // Update pre[i]
      if((arr[i - 1] > arr[i]))
      {
          pre[i] = pre[i - 1] + 1;
      }
      else
      {  pre[i] = pre[i - 1];}
    }
  
    // Traverse the array Q[][]
    for (int i = 0; i < Q.GetLength(0); i++)
    {
         
      int l = Q[i,0];
      int r = Q[i,1] - 1;
  
      // If pre[r] - pre[l-1] exceeds 0
      if (pre[r] - pre[l - 1] == 0)
      {  Console.WriteLine("Yes");}
      else
        {Console.WriteLine("No");}
    }
    }
     
    // Driver Code
 
    static public void Main (){
         
        int[] arr = { 1, 7, 3, 4, 9 };
    int[,] Q = { { 1, 2 }, { 2, 4 } };
  
    int N = arr.Length;
  
    // Function Call
    checkSorted(arr, N, Q);
    }
}
 
// This code is contributed by avanitrachhadiya2155


输出:
Yes
No

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

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