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++
#include <bits/stdc++.h>
using namespace std;
void checkCount( int A[], int Q[][2],
int q)
{
for ( int i = 0; i < q; i++) {
int L = Q[i][0];
int R = Q[i][1];
L--, R--;
if ((A[L] < A[L + 1])
!= (A[R - 1] < A[R])) {
cout << "Yes\n" ;
}
else {
cout << "No\n" ;
}
}
}
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
import java.io.*;
import java.util.*;
class GFG{
static void checkCount( int A[], int Q[][],
int q)
{
for ( int i = 0 ; i < q; i++)
{
int L = Q[i][ 0 ];
int R = Q[i][ 1 ];
L--;
R--;
if ((A[L] < A[L + 1 ]) !=
(A[R - 1 ] < A[R]))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
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);
}
}
|
Python3
def checkCount(A, Q, q):
for i in range (q):
L = Q[i][ 0 ]
R = Q[i][ 1 ]
L - = 1
R - = 1
if ((A[L] < A[L + 1 ]) ! =
(A[R - 1 ] < A[R])):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
arr = [ 11 , 13 , 12 , 14 ]
Q = [ [ 1 , 4 ], [ 2 , 4 ] ]
q = len (Q)
checkCount(arr, Q, q)
|
C#
using System;
class GFG{
static void checkCount( int [] A, int [,] Q,
int q)
{
for ( int i = 0; i < q; i++)
{
int L = Q[i, 0];
int R = Q[i, 1];
L--;
R--;
if ((A[L] < A[L + 1]) !=
(A[R - 1] < A[R]))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
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);
}
}
|
Javascript
<script>
function checkCount(A, Q, q)
{
for (let i = 0; i < q; i++)
{
let L = Q[i][0];
let R = Q[i][1];
L--;
R--;
if ((A[L] < A[L + 1]) !=
(A[R - 1] < A[R]))
{
document.write( "Yes" + "<br/>" );
}
else
{
document.write( "No" + "<br/>" );
}
}
}
let arr = [ 11, 13, 12, 14 ];
let Q = [[ 1, 4 ], [ 2, 4 ]];
let q = Q.length;
checkCount(arr, Q, q);
</script>
|
Time Complexity: O(Q)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...