Program to calculate Bitonicity of an Array

The bitonicity of an array is a measure that indicates how much the array deviates from a monotonic sequence. It starts at 0 and increases by 1 when an element is greater than the previous one, decreases by 1 when smaller, and remains unchanged when equal.

Syntax

int calculateBitonicity(int arr[], int n);

Algorithm

The bitonicity calculation follows this logic −

Bitonicity = 0 (initially)
For i from 1 to n-1:
    if arr[i] > arr[i-1]: Bitonicity = Bitonicity + 1
    if arr[i] < arr[i-1]: Bitonicity = Bitonicity - 1  
    if arr[i] = arr[i-1]: Bitonicity remains unchanged

Example

The following program demonstrates how to calculate the bitonicity of an array by comparing consecutive elements −

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 4, 5, 4, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    int bitonicity = 0;
    
    printf("Array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br>");
    
    for (int i = 1; i < n; i++) {
        if (arr[i] > arr[i - 1]) {
            bitonicity++;
            printf("arr[%d]=%d > arr[%d]=%d, bitonicity = %d<br>", 
                   i, arr[i], i-1, arr[i-1], bitonicity);
        }
        else if (arr[i] < arr[i - 1]) {
            bitonicity--;
            printf("arr[%d]=%d < arr[%d]=%d, bitonicity = %d<br>", 
                   i, arr[i], i-1, arr[i-1], bitonicity);
        }
        else {
            printf("arr[%d]=%d = arr[%d]=%d, bitonicity = %d<br>", 
                   i, arr[i], i-1, arr[i-1], bitonicity);
        }
    }
    
    printf("\nFinal Bitonicity = %d<br>", bitonicity);
    return 0;
}
Array: 1 2 4 5 4 3 
arr[1]=2 > arr[0]=1, bitonicity = 1
arr[2]=4 > arr[1]=2, bitonicity = 2
arr[3]=5 > arr[2]=4, bitonicity = 3
arr[4]=4 < arr[3]=5, bitonicity = 2
arr[5]=3 < arr[4]=4, bitonicity = 1

Final Bitonicity = 1

Key Points

  • Bitonicity starts at 0 and changes based on element comparisons
  • A positive bitonicity indicates the array has more increasing trends
  • A negative bitonicity indicates more decreasing trends
  • Zero bitonicity means equal increasing and decreasing patterns

Conclusion

Bitonicity provides a simple metric to analyze the overall trend of an array. The final value indicates whether the array has predominantly increasing or decreasing patterns.

Updated on: 2026-03-15T11:20:11+05:30

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements