# Queries to check if count of increasing and decreasing subarrays is same in given range

• Last Updated : 22 Apr, 2021

Given an array arr[] consisting of N integers and an array Q[][], where each row is a query of the form {L, R}. The task for each query is to check if the count of increasing and decreasing subarrays in the range [L, R] is the same or not. If it is found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

Input: arr[] = {11, 13, 12, 14}, Q[][] = {{1, 4}, {2, 4}}
Output:
No
Yes
Explanation:
Query 1: There are two increasing subarrays {11, 13} and {12, 14} in the range [1, 4]. But there is only one decreasing subarray {13, 12} in the range [1, 4].
Therefore, print No.
Query 2: There is only one increasing subarray {12, 14} and one decreasing subarray {13, 12} in the range [2, 4].
Therefore, print Yes.

Input: arr[] = {16, 24, 32, 18, 14}, Q = {{1, 5}, {2, 3}, {2, 4}}
Output:
Yes
No
Yes

Naive Approach: The simplest approach to solve this problem is to generate all possible subarrays from the subarray {arr[L], arr[R]} and check if the count of increasing and decreasing subarrays is the same or not. If found to be true, then print “Yes”. Otherwise, print “No”

Time Complexity: O(Q*N2)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, observe that every increasing subarray is followed by a decreasing subarray and vice versa i.e., they alternately increase or decrease. Therefore, the number of increasing and decreasing subarrays will differ by at most 1. Therefore, the idea is to check if the first pair and last pair of elements from the range, both forms increasing pairs or not. If found to be true, then print “No”. Otherwise, print “Yes”. Perform the above step for each query to get the result in O(1).

Below is the implementation of the above approach:

## 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[],``                ``int` `q)``{` `    ``// Traverse each query``    ``for` `(``int` `i = 0; i < q; i++) {` `        ``int` `L = Q[i];``        ``int` `R = Q[i];` `        ``// 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[] = { { 1, 4 }, { 2, 4 } };` `    ``int` `q = ``sizeof``(Q) / ``sizeof``(Q);` `    ``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

 ``

Output:

```No
Yes```

Time Complexity: O(Q)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up