![]() |
VOOZH | about |
Given an array arr[] of size n containing positive integers, return the maximum length of the bitonic subarray.
A subarray arr[i...j] is considered bitonic if its elements first monotonically increase, and then monotonically decrease. Formally, there exists and index k (where i <= k <= j) such that:
Examples
Input: arr[] = [12, 4, 78, 90, 45, 23]
Output: 5
Explanation: The longest bitonic subarray is[4, 78, 90, 45, 23]. It starts increasing at4, peaks at90, and decreases to23, giving a length of 5.
Input: arr[] = [10, 20, 30, 40]
Output: 4
Explanation: The array[10, 20, 30, 40]is strictly increasing with no decreasing part, so the longest bitonic subarray is the entire array itself, giving a length of 4.
Table of Content
We check all possible subarrays, trying each index as a peak. If a subarray follows the bitonic pattern, we update the longest length found.
5
For solving this problem we can imagine each number as the peak of a mountain. For every element, count how many consecutive numbers before it are rising (or flat) and how many after it are falling (or flat). Add these two counts and subtract one (to avoid counting the peak twice). The longest mountain found this way is answer.
5
The idea is to find the longest bitonic subarray starting from each index arr[i]. From arr[i], the process first detects the end of the ascent (increasing sequence), then the descent (decreasing sequence). To handle overlapping subarrays, we record the nextStart position when two equal values are encountered during the descent phase. If the length of the current bitonic subarray exceeds the previously recorded maxLen, we update it.
Implementation:
j as long as the sequence is non-decreasing (arr[j] <= arr[j+1]). This finds the peak.j as long as the sequence is non-increasing (arr[j] >= arr[j+1]).start = nextStart to prepare for the next iteration of the main loop.Example:
arr[]:[12, 4, 78, 90, 45, 23] Initial State: maxLen = 1, start = 0, nextStart = 0, j= 0
j to 1 (12 > 4) and sets nextStart = 1 → maxLen = max(1,1-0+1) =2 → start=1.j to 3 (4 < 78 < 90) → Descent advances j to 5 (90 > 45 > 23), updating nextStart = 5 → maxLen = max(2,5-1+1) → start = 5.5.5