
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to calculate Bitonicity of an Array
Given with an array of integers and the task is to calculate the bitonicity of a given array using a function.
Bitonicity of an array is −
- Initialised to 0
- Incremented to 1 when the next element is greater than the previous value
- Decremented to 1 when the next element is lesser than the previous value
Example
Input-: arr[] = { 1,4,3,5,2,9,10,11} Output-: Bitonicity of an array is : 3
Explanation −
- Initialize bitonicity calculating variable let’s say temp with 0.
- Start from the first element of an array which is 1. Now compare arr[i] and arr[i-1] i.e. compare 4 and 1 here 4 is greater than 1 thereby increment temp with 1. Similarly compare 4 and 3 since 3 is lesser than 4 decrement the value of temp.
- Print the final value of temp which is 3
Approach used in the below program is as follows
- Traverse all the elements of an array let’s say arr[n] where n is size of an array
- If arr[i] > arr[i-1], than bitonicity = bitonicity + 1
- If arr[i] < arr[i-1], than bitonicity = bitonicity – 1
- If arr[i] = arr[i-1], than bitonicity = bitonicity (unchanged)
Algorithm
Start Step 1-> Declare function to calculate bitonicity of an array int cal_bitonicity(int arr[], int n) set int temp = 0 Loop For int i = 1 and i < n and i++ IF (arr[i] > arr[i - 1]) Increment temp++ End Else IF (arr[i] < arr[i - 1]) Decrement temp— End return temp step 2-> In main() declare int arr[] = { 1,4,3,5,2,9,10,11} set int n = sizeof(arr) / sizeof(arr[0]) Call cal_bitonicity(arr, n) Stop
Example
#include <iostream> using namespace std; // calculate bitonicity int cal_bitonicity(int arr[], int n) { int temp = 0; for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) temp++; else if (arr[i] < arr[i - 1]) temp--; } return temp; } int main() { int arr[] = { 1,4,3,5,2,9,10,11}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Bitonicity of an array is : " <<cal_bitonicity(arr, n); return 0; }
Output
IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT
Bitonicity of an array is : 3
- Related Articles
- Program to calculate Bitonicity of an Array
- Java Program to calculate the time of sorting an array
- PHP program to calculate the total time given an array of times
- Program to calculate area of Circumcircle of an Equilateral Triangle
- Swift Program to Calculate the sum of Elements in a Given Array
- Program to calculate area of Circumcircle of an Equilateral Triangle in C++
- Program to print Sum Triangle of an array.
- Golang Program to Rotate Elements of an Array
- Program to calculate the area of an Circle inscribed in a Square
- Write a Golang program to calculate the sum of elements in a given array
- Program to calculate area of Enneagon
- Program to calculate Area Of Octagon
- Java program to reverse an array
- C# program to reverse an array
- Golang program to print an array?

Advertisements