Count of all possible bitonic subarrays
Last Updated :
20 May, 2021
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++
// C++ program to count the
// number of possible
// bitonic subarrays
#include <bits/stdc++.h>
using namespace std;
// Function to return the count
// of bitonic subarrays
void countbitonic(int arr[], int n)
{
int c = 0;
// Starting element of subarray
for (int i = 0; i < n; i++) {
// Ending element of subarray
for (int j = i; j < n; j++) {
int temp = arr[i], f = 0;
// for 1 length
if (j == i) {
c++;
continue;
}
int k = i + 1;
// For increasing sequence
while (temp < arr[k] && k <= j) {
temp = arr[k];
k++;
}
// If strictly increasing
if (k > j) {
c++;
f = 2;
}
// For decreasing sequence
while (temp > arr[k]
&& k <= j
&& f != 2) {
temp = arr[k];
k++;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
cout << c << endl;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 4, 3, 6, 5 };
int N = 6;
countbitonic(arr, N);
}
Java
// Java program to count the number
// of possible bitonic subarrays
import java.io.*;
import java.util.*;
class GFG{
// Function to return the count
// of bitonic subarrays
public static void countbitonic(int arr[], int n)
{
int c = 0;
// Starting element of subarray
for(int i = 0; i < n; i++)
{
// Ending element of subarray
for(int j = i; j < n; j++)
{
int temp = arr[i], f = 0;
// For 1 length
if (j == i)
{
c++;
continue;
}
int k = i + 1;
// For increasing sequence
while (temp < arr[k] && k <= j)
{
temp = arr[k];
k++;
}
// If strictly increasing
if (k > j)
{
c++;
f = 2;
}
// For decreasing sequence
while ( k <= j && temp > arr[k] && f != 2)
{
temp = arr[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = 0;
}
}
}
System.out.println(c);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 4, 3, 6, 5 };
int N = 6;
countbitonic(arr, N);
}
}
// This code is contributed by grand_master
Python3
# Python3 program to count the number
# of possible bitonic subarrays
# Function to return the count
# of bitonic subarrays
def countbitonic(arr, n):
c = 0;
# Starting element of subarray
for i in range(n):
# Ending element of subarray
for j in range(i, n):
temp = arr[i]
f = 0;
# For 1 length
if (j == i) :
c += 1
continue;
k = i + 1;
# For increasing sequence
while (temp < arr[k] and k <= j):
temp = arr[k];
k += 1
# If strictly increasing
if (k > j) :
c += 1
f = 2;
# For decreasing sequence
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)
# Driver Code
arr = [ 1, 2, 4, 3, 6, 5 ];
N = 6;
countbitonic(arr, N);
# This code is contributed by grand_master
C#
// C# program to count the number
// of possible bitonic subarrays
using System;
class GFG{
// Function to return the count
// of bitonic subarrays
public static void countbitonic(int []arr, int n)
{
int c = 0;
// Starting element of subarray
for(int i = 0; i < n; i++)
{
// Ending element of subarray
for(int j = i; j < n; j++)
{
int temp = arr[i], f = 0;
// for 1 length
if (j == i)
{
c++;
continue;
}
int k = i + 1;
// For increasing sequence
while (temp < arr[k] && k <= j)
{
temp = arr[k];
k++;
}
// If strictly increasing
if (k > j)
{
c++;
f = 2;
}
// For decreasing sequence
while ( k <= j && temp > arr[k] && f != 2)
{
temp = arr[k];
k++;
}
if (k > j && f != 2)
{
c++;
f = 0;
}
}
}
Console.Write(c);
}
// Driver code
public static void Main()
{
int[] arr = { 1, 2, 4, 3, 6, 5 };
int N = 6;
countbitonic(arr, N);
}
}
// This code is contributed by grand_master
JavaScript
<script>
// Javascript program to count the
// number of possible
// bitonic subarrays
// Function to return the count
// of bitonic subarrays
function countbitonic(arr, n)
{
var c = 0;
// Starting element of subarray
for (var i = 0; i < n; i++)
{
// Ending element of subarray
for (var j = i; j < n; j++)
{
var temp = arr[i], f = 0;
// for 1 length
if (j == i)
{
c++;
continue;
}
var k = i + 1;
// For increasing sequence
while (temp < arr[k] && k <= j) {
temp = arr[k];
k++;
}
// If strictly increasing
if (k > j) {
c++;
f = 2;
}
// For decreasing sequence
while (temp > arr[k]
&& k <= j
&& f != 2) {
temp = arr[k];
k++;
}
if (k > j && f != 2) {
c++;
f = 0;
}
}
}
document.write( c );
}
// Driver Code
var arr = [1, 2, 4, 3, 6, 5];
var N = 6;
countbitonic(arr, N);
// This code is contributed by rutvik_56.
</script>
Time Complexity: O (N 3)
Auxiliary Space: O (1)
Similar Reads
Count of all possible reverse bitonic subarrays 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
8 min read
Count distinct Bitwise OR of all Subarrays Given an array A of non-negative integers, where 0 \leq A[i] \leq 10^{9} . The task is to count number of distinct possible results obtained by taking the bitwise OR of all the elements in all possible Subarrays. Examples: Input: A = [1, 2] Output: 3 Explanation: The possible subarrays are [1], [2],
7 min read
Sum of bitwise OR of all subarrays Given an array of positive integers, find the total sum after performing the bit wise OR operation on all the sub arrays of a given array. Examples: Input : 1 2 3 4 5 Output : 71 Input : 6 5 4 3 2 Output : 84 First initialize the two variable sum=0, sum1=0, variable sum will store the total sum and,
5 min read
Count Subarrays of 1 in Binary Array Given an array arr[] of size N, the array contains only 1s and 0s, and the task is to return the count of the total number of subarrays where all the elements of the subarrays are 1. Examples: Input: N = 4, arr[] = {1, 1, 1, 0}Output: 6Explanation: Subarrays of 1 will look like the following: [1], [
12 min read
Sum of bitwise AND of all subarrays Given an array consisting of N positive integers, find the sum of bit-wise and of all possible sub-arrays of the array. Examples: Input : arr[] = {1, 5, 8} Output : 15 Bit-wise AND of {1} = 1 Bit-wise AND of {1, 5} = 1 Bit-wise AND of {1, 5, 8} = 0 Bit-wise AND of {5} = 5 Bit-wise AND of {5, 8} = 0
8 min read