Count of all possible bitonic subarrays
Given an array arr[] consisting of N integers, the task is to count all the subarrays which are Bitonic in nature.
A bitonic subarray is a subarray in which elements are either strictly increasing or strictly decreasing, or are first increasing and then decreasing.
Examples:
Input: arr[] = {2, 1, 4, 5}
Output: 8
Explanation:
All subarray which are bitonic in subarray are : {2}, {2, 1}, {1}, {1, 4}, {1, 4, 5}, {4}, {4, 5} and {5}.
Input: arr[] = {1, 2, 3, 4}
Output:10
Approach:
Follow the steps below to solve the problems:
- Generate all possible subarrays.
- For each subarray, check if it is bitonic or not. If the subarray is Bitonic then increment count for answer.
- Finally return the answer.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void countbitonic( 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[] = { 1, 2, 4, 3, 6, 5 };
int N = 6;
countbitonic(arr, N);
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static void countbitonic( 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.println(c);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 3 , 6 , 5 };
int N = 6 ;
countbitonic(arr, N);
}
}
|
Python3
def countbitonic(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 (temp < arr[k] and k < = j):
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 = [ 1 , 2 , 4 , 3 , 6 , 5 ];
N = 6 ;
countbitonic(arr, N);
|
C#
using System;
class GFG{
public static void countbitonic( 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()
{
int [] arr = { 1, 2, 4, 3, 6, 5 };
int N = 6;
countbitonic(arr, N);
}
}
|
Javascript
<script>
function countbitonic(arr, n)
{
var c = 0;
for ( var i = 0; i < n; i++)
{
for ( var j = i; j < n; j++)
{
var temp = arr[i], f = 0;
if (j == i)
{
c++;
continue ;
}
var 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;
}
}
}
document.write( c );
}
var arr = [1, 2, 4, 3, 6, 5];
var N = 6;
countbitonic(arr, N);
</script>
|
Time Complexity: O (N 3)
Auxiliary Space: O (1)
Last Updated :
20 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...