Open In App

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

Last Updated : 22 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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




<script>
 
// JavaScript program for the above approach
 
// Function to check if given range
// have equal number of increasing
// as well as decreasing subarrays
function checkCount(A, Q, q)
{
      
    // Traverse each query
    for(let i = 0; i < q; i++)
    {
        let L = Q[i][0];
        let 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]))
        {
            document.write("Yes" + "<br/>");
        }
        else
        {
            document.write("No" + "<br/>");
        }
    }
}
 
// Driver Code
 
    let arr = [ 11, 13, 12, 14 ];
    let Q = [[ 1, 4 ], [ 2, 4 ]];
   
    let q = Q.length;
   
    checkCount(arr, Q, q);
     
</script>


Output: 

No
Yes

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads