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

📅  最后修改于: 2021-05-17 03:18:39             🧑  作者: Mango

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

例子:

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

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

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

下面是上述方法的实现:

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)