You are given a bitonic array nums of length n.
Split the array into two parts:
n - 1 (inclusive).The peak element belongs to both parts.
Return:
Notes:
Example 1:
Input: nums = [1,3,2,1]
Output: 1
Explanation:
nums[1] = 3[1, 3], sum is 1 + 3 = 4[3, 2, 1], sum is 3 + 2 + 1 = 6Example 2:
Input: nums = [2,4,5,2]
Output: 0
Explanation:
nums[2] = 5[2, 4, 5], sum is 2 + 4 + 5 = 11[5, 2], sum is 5 + 2 = 7Example 3:
Input: nums = [1,2,4,3]
Output: -1
Explanation:
nums[2] = 4[1, 2, 4], sum is 1 + 2 + 4 = 7[4, 3], sum is 4 + 3 = 7
Constraints:
3 <= n == nums.length <= 1051 <= nums[i] <= 109nums is a bitonic array.Problem Overview: You are given a sequence that forms a bitonic pattern: values increase up to a peak and then decrease. The task is to compare the total sum of the increasing portion with the total sum of the decreasing portion and determine which side contributes more.
Approach 1: Brute Force Split Check (O(n^2) time, O(1) space)
Check every index as a possible peak. For each candidate, verify whether elements to the left are strictly increasing and elements to the right are strictly decreasing. If the condition holds, compute the sum of both parts using iteration. This method repeatedly scans the array for validation and summation, which leads to quadratic time complexity. Useful mainly to reason about the problem constraints before optimizing.
Approach 2: Single Pass Peak Detection (O(n) time, O(1) space)
If the array is guaranteed to be bitonic, the peak can be found by scanning once until the increasing trend stops. While iterating, accumulate the sum of the increasing part. After the peak index, continue the scan and accumulate the decreasing part. Only one traversal is required, and you maintain two running totals. This approach relies on simple iteration and works well for problems involving arrays with a single bitonic transition.
Approach 3: Prefix Sum with Peak Detection (O(n) time, O(n) space)
Build a prefix sum array where prefix[i] stores the sum of elements from index 0 to i. Once the peak index is located using a linear scan, compute the sum of the increasing segment and decreasing segment in constant time using prefix arithmetic. This avoids repeated summation and keeps the logic clean when the array size is large or when multiple comparisons are required.
Approach 4: Binary Search Peak + Prefix Sum (O(log n) time, O(n) space)
For strictly bitonic arrays, the peak can be located using binary search. Compare nums[mid] with neighbors to determine whether you are in the increasing or decreasing slope and move the search window accordingly. After identifying the peak, use prefix sums to compute both segment totals instantly. This reduces peak detection from linear to logarithmic time.
Recommended for interviews: The single-pass scan is usually expected because it is simple and runs in O(n) time with constant space. Showing the brute force approach demonstrates problem understanding, but the linear scan highlights practical optimization. Mentioning the binary search variant shows awareness of bitonic array properties, which interviewers often appreciate.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Split Check | O(n^2) | O(1) | Useful for understanding the bitonic property or when constraints are very small |
| Single Pass Peak Detection | O(n) | O(1) | Best general solution when the array is already bitonic |
| Prefix Sum + Peak Scan | O(n) | O(n) | When repeated sum queries or cleaner segment calculations are required |
| Binary Search Peak + Prefix Sum | O(log n) | O(n) | When the array is strictly bitonic and faster peak discovery is desired |
Practice Compare Sums of Bitonic Parts with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor