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 −
Input-: arr[] = { 1,4,3,5,2,9,10,11} Output-: Bitonicity of an array is : 3
Explanation −
Approach used in the below program is as follows
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
#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; }
IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT
Bitonicity of an array is : 3