C++ Program to calculate the Bitonicity of an Array

In this article, we will discuss a C++ program to calculate the Bitonicity of an Array.

The Bitonicity of an array is -

  1. Initialized to 0
  2. Incremented to one if the subsequent element is more than the previous value.
  3. Decremented to 1 if the following element is lesser than the preceding value.

Example:

  • Initialize the bitonicity calculation variable's initial value to 0, let's assume temp.
  • Begin with element 1, the first entry in an array. Examine arr[i] and arr[i-1]. Here, compare 5 and 1. As 5 is larger than 1, the temp is increased by 1. In the same way, compare 5 to 4; as 4 is less than 5, the temp is decreased by 1.
  • Print the temp final value, which is 3.

An approach to find the bitonicity of an array:

  • Go through each element in an array, let's say arr[n], where n is the array's size.
  • if arr[i] > arr[i-1] , bitonicity = bitonicity + 1
  • if arr[i] < arr[i-1], bitonicity = bitonicity - 1
  • In the case, when arr[i] = arr[i-1], bitonicity remains constant.

Algorithm:

Program:

Let us take an example to calculate the Bitnocity of an array in C++

Output:

C++ Program to calculate the Bitonicity of an Array

Complexity Analysis:

Time Complexity: O(N), since the bitonicity of the array is found by traversing it N times through a loop.

Space Complexity: O(1), Since no additional space complexity is being used.