Count of all possible reverse bitonic subarrays
Last Updated :
09 Jun, 2021
Given an array arr[] of N integers, the task is to count the total number of Reverse Bitonic Subarray from the given array.
A Reverse Bitonic Subarray is a subarray in which elements are arranged in decreasing order and then arranged in increasing order. A strictly increasing or strictly decreasing subarray is also considered as Reverse Bitonic Subarray.
Examples:
Input: arr[] = {2, 3, 1, 4}
Output: 8
Explanation:
Here we will look for all length’s subarrays of given array
For length 1, all the subarrays are reverse bitonic subarray {2}, {3}, {1}, {4}
For length 2, possible subarrays are {2, 3}, {3, 1}, {1, 4}
For length 3, possible subarray is {3, 1, 4}
So in total, there are 8 subarrays possible.
Input: arr[] = [1, 2, 3]
Output: 6
Explanation:
Here we will look for all length’s subarrays of given array
For length 1, all the subarrays are reverse bitonic {1}, {2}, {3}
For length 2, possible subarrays are {1, 2}, {2, 3}
For length 3, possible subarray is {1, 2, 3}.
So in total, there are 6 subarrays possible.
Approach: The idea is to generate all the subarrays from the given array and check if each subarray satisfy the below mentioned conditions:
- When the subarray elements are strictly increasing, then take the first element and then check for next to be increasing.
- When the subarray elements are strictly decreasing, then take the first element and then check for next to be decreasing.
- When the subarray elements are strictly decreasing then increasing, then in that case, take the first element and then check for next to decreasing and when it becomes false then check for increasing till the last element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countReversebitonic( int arr[],
int n)
{
int c = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
int temp = arr[i], f = 0;
if (j == i) {
c++;
continue ;
}
int k = i + 1;
while (temp > arr[k]
&& k <= j) {
temp = arr[k];
k++;
}
if (k > j) {
c++;
f = 2;
}
while (temp < arr[k]
&& k <= j && f != 2) {
temp = arr[k];
k++;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
cout << c << endl;
}
int main()
{
int arr[] = { 2, 3, 1, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
countReversebitonic(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void countReversebitonic( int arr[],
int n)
{
int c = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int temp = arr[i], f = 0 ;
if (j == i)
{
c++;
continue ;
}
int k = i + 1 ;
while (temp > arr[k] && k <= j)
{
temp = arr[k];
k++;
}
if (k > j)
{
c++;
f = 2 ;
}
while (k <= j && temp < arr[k] &&
f != 2 )
{
temp = arr[k];
k++;
}
if (k > j && f != 2 )
{
c++;
f = 0 ;
}
}
}
System.out.print(c + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 1 , 4 };
int N = arr.length;
countReversebitonic(arr, N);
}
}
|
Python3
def countReversebitonic(arr, n):
c = 0 ;
for i in range (n):
for j in range (i, n):
temp = arr[i]
f = 0 ;
if (j = = i):
c + = 1 ;
continue ;
k = i + 1 ;
while (k < = j and temp > arr[k]):
temp = arr[k];
k + = 1 ;
if (k > j):
c + = 1 ;
f = 2 ;
while (k < = j and temp < arr[k] and
f ! = 2 ):
temp = arr[k];
k + = 1 ;
if (k > j and f ! = 2 ):
c + = 1 ;
f = 0 ;
print (c)
arr = [ 2 , 3 , 1 , 4 ];
countReversebitonic(arr, len (arr));
|
C#
using System;
class GFG{
static void countReversebitonic( int []arr,
int n)
{
int c = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
int temp = arr[i], f = 0;
if (j == i)
{
c++;
continue ;
}
int k = i + 1;
while (temp > arr[k] && k <= j)
{
temp = arr[k];
k++;
}
if (k > j)
{
c++;
f = 2;
}
while (k <= j && temp < arr[k] &&
f != 2)
{
temp = arr[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = 0;
}
}
}
Console.Write(c);
}
public static void Main( string [] args)
{
int []arr = { 2, 3, 1, 4 };
int N = arr.Length;
countReversebitonic(arr, N);
}
}
|
Javascript
<script>
function countReversebitonic(arr, n)
{
let c = 0;
for (let i = 0; i < n; i++)
{
for (let j = i; j < n; j++)
{
let temp = arr[i], f = 0;
if (j == i)
{
c++;
continue ;
}
let k = i + 1;
while (temp > arr[k] && k <= j)
{
temp = arr[k];
k++;
}
if (k > j)
{
c++;
f = 2;
}
while (k <= j && temp < arr[k] &&
f != 2)
{
temp = arr[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = 0;
}
}
}
document.write(c + "<br/>" );
}
let arr = [ 2, 3, 1, 4 ];
let N = arr.length;
countReversebitonic(arr, N);
</script>
|
Time Complexity: O(N2), where N is the number of elements in the given array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...