📌  相关文章
📜  查询以检查在给定范围内递增和递减子数组的数量是否相同

📅  最后修改于: 2021-05-17 16:52:42             🧑  作者: Mango

给定一个由N个整数组成的数组arr []和一个数组Q [] [] ,其中每一行都是{L,R}形式的查询。每个查询的任务是检查[L,R]范围内的递增和递减子数组的计数是否相同。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

天真的方法:解决此问题的最简单方法是从子数组{arr [L],arr [R]}生成所有可能的子数组,并检查递增和递减子数组的计数是否相同。如果发现是真的,则打印“是” 。否则,打印“否”

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

高效方法:要优化上述方法,请注意,每个增加的子数组后面都有一个减少的子数组,反之亦然,即它们交替增加或减少。因此,增加和减少子数组的数量最多相差1 。因此,该想法是检查范围中的第一对元素和最后一对元素是否都形成递增对。如果发现是真的,则打印“否” 。否则,打印“是” 。对每个查询执行以上步骤,以在O(1)中获得结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
void checkCount(int A[], int Q[][2],
                int q)
{
 
    // Traverse each query
    for (int i = 0; i < q; i++) {
 
        int L = Q[i][0];
        int R = Q[i][1];
 
        // For 0-based indexing
        L--, R--;
 
        // Condition for same count of
        // increasing & decreasing subarray
        if ((A[L] < A[L + 1])
            != (A[R - 1] < A[R])) {
            cout << "Yes\n";
        }
        else {
            cout << "No\n";
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 11, 13, 12, 14 };
    int Q[][2] = { { 1, 4 }, { 2, 4 } };
 
    int q = sizeof(Q) / sizeof(Q[0]);
 
    checkCount(arr, Q, q);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
   
class GFG{
   
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
static void checkCount(int A[], int Q[][],
                       int q)
{
     
    // Traverse each query
    for(int i = 0; i < q; i++)
    {
        int L = Q[i][0];
        int R = Q[i][1];
  
        // For 0-based indexing
        L--;
        R--;
  
        // Condition for same count of
        // increasing & decreasing subarray
        if ((A[L] < A[L + 1]) !=
            (A[R - 1] < A[R]))
        {
            System.out.println("Yes");
        }
        else
        {
            System.out.println("No");
        }
    }
}
   
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 11, 13, 12, 14 };
    int Q[][] = { { 1, 4 }, { 2, 4 } };
  
    int q = Q.length;
  
    checkCount(arr, Q, q);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to check if given range
# have equal number of increasing
# as well as decreasing subarrays
def checkCount(A, Q, q):
     
    # Traverse each query
    for i in range(q):
        L = Q[i][0]
        R = Q[i][1]
 
        # For 0-based indexing
        L -= 1
        R -= 1
 
        # Condition for same count of
        # increasing & decreasing subarray
        if ((A[L] < A[L + 1]) !=
            (A[R - 1] < A[R])):
            print("Yes")
        else:
            print("No")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 11, 13, 12, 14 ]
    Q = [ [ 1, 4 ], [ 2, 4 ] ]
 
    q = len(Q)
 
    checkCount(arr, Q, q)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
    
class GFG{
    
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
static void checkCount(int[] A, int[,] Q,
                       int q)
{
     
    // Traverse each query
    for(int i = 0; i < q; i++)
    {
        int L = Q[i, 0];
        int R = Q[i, 1];
   
        // For 0-based indexing
        L--;
        R--;
   
        // Condition for same count of
        // increasing & decreasing subarray
        if ((A[L] < A[L + 1]) !=
            (A[R - 1] < A[R]))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
    
// Driver Code
public static void Main()
{
    int[] arr = { 11, 13, 12, 14 };
    int[,] Q = { { 1, 4 }, { 2, 4 } };
   
    int q = Q.GetLength(0);
   
    checkCount(arr, Q, q);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
No
Yes

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