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

📅  最后修改于: 2021-09-06 05:22:49             🧑  作者: Mango

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

例子:

朴素的方法:解决这个问题最简单的方法是从子数组{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)

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