Open In App

How do you know if an array is bitonic?

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A bitonic array is an array that consists of a monotonically increasing sequence followed by a monotonically decreasing sequence. In other words, it has a single peak element, after which the elements decrease in value.

Identifying the Peak Element

The first step in determining if an array is bitonic is to identify the peak element. This can be done using a binary search algorithm.

  • Initialize two pointers, left and right, to the first and last elements of the array, respectively.
  • While left is less than or equal to right:
    • Calculate the midpoint mid of the range [left, right].
    • If the element at index mid is greater than its neighbors (i.e., arr[mid] > arr[mid-1] and arr[mid] > arr[mid+1]), then mid is the peak element.
    • If the element at index mid is less than its right neighbor, then the peak element must be in the right half of the array. Set left to mid + 1.
    • Otherwise, the peak element must be in the left half of the array. Set right to mid – 1.

Checking for Monotonicity

Once the peak element has been identified, we need to check if the array is monotonically increasing before the peak and monotonically decreasing after the peak.

  • Increasing Sequence: Check if the elements in the range [0, peak_index] are monotonically increasing. This can be done by iterating through the range and checking if each element is greater than the previous one.
  • Decreasing Sequence: Check if the elements in the range [peak_index + 1, n-1] are monotonically decreasing. This can be done by iterating through the range and checking if each element is less than the previous one.

Conclusion:

If both the increasing and decreasing sequences are confirmed, then the array is bitonic. Otherwise, it is not bitonic.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads