2. Bitonic Point
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given an array arr[]
of n
elements that is first strictly increasing and then (optionally) strictly decreasing, return the Bitonic Point โ the maximum element in the array. It is guaranteed that the array contains exactly one bitonic point.
A Bitonic Point is the element where the sequence changes from increasing to decreasing โ or simply the largest number in a bitonic array.
Edge Cases:
Entirely increasing โ last element is peak.
Entirely decreasing โ first element is peak.
๐ Examples
Example 1:
Input:
arr = [1, 2, 4, 5, 7, 8, 3]
Output:
8
Explanation:
Increasing:
[1, 2, 4, 5, 7]
Peak:
8
Decreasing:
[3]
Example 2:
Input:
arr = [10, 20, 30, 40, 50]
Output:
50
Explanation:
Increasing:
[10, 20, 30, 40]
Peak:
50
Decreasing:
[]
Example 3:
Input:
arr = [120, 100, 80, 20, 0]
Output:
120
Explanation:
Increasing:
[]
Peak:
120
Decreasing:
[100, 80, 20, 0]
๐ Constraints
$3 \leq n \leq 10^5$
$1 \leq arr[i] \leq 10^6$
โ
My Approach
Optimized Approach: Binary Search on Bitonic Pattern
We observe that:
If
arr[mid] > arr[mid+1]
, then the maximum lies to the left (including mid).If
arr[mid] < arr[mid+1]
, then the maximum lies to the right.
This is a classical bitonic binary search:
We use a variant of binary search to reduce the search space logarithmically.
We stop when we find the peak element such that
arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]
.
Algorithm Steps:
Initialize
low = 0
,high = n - 1
.While
low < high
:Compute
mid = (low + high) // 2
.If
arr[mid] < arr[mid + 1]
: Move right โlow = mid + 1
Else: Move left (may include mid) โ
high = mid
Return
arr[low]
as the maximum bitonic point.
๐งฎ Time and Auxiliary Space Complexity
Expected Time Complexity: O(log n), as we use binary search to find the peak.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional variables.
๐ง Code (C++)
class Solution {
public:
int findMaximum(vector<int> &arr) {
int low = 0, high = arr.size() - 1;
while (low < high) {
int mid = (low + high) / 2;
if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid;
}
return arr[low];
}
};
๐งโ๐ป Code (Java)
class Solution {
public int findMaximum(int[] arr) {
int low = 0, high = arr.length - 1;
while (low < high) {
int mid = (low + high) / 2;
if (arr[mid] < arr[mid + 1])
low = mid + 1;
else
high = mid;
}
return arr[low];
}
}
๐ Code (Python)
class Solution:
def findMaximum(self, arr):
low, high = 0, len(arr) - 1
while low < high:
mid = (low + high) // 2
if arr[mid] < arr[mid + 1]:
low = mid + 1
else:
high = mid
return arr[low]
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated