[LeetCode] Compare Sums of Bitonic Parts

3909. Compare Sums of Bitonic Parts

You are given a bitonic array nums of length n.

Split the array into two parts:

  • Ascending part: from index 0 to the peak element (inclusive).
  • Descending part: from the peak element to index n - 1 (inclusive).

The peak element belongs to both parts.

Return:

  • 0 if the sum of the ascending part is greater.
  • 1 if the sum of the descending part is greater.
  • -1 if both sums are equal.

Notes:

  • A bitonic array is an array that is strictly increasing up to a single peak element and then strictly decreasing.
  • An array is said to be strictly increasing if each element is strictly greater than its previous one (if exists).
  • An array is said to be strictly decreasing if each element is strictly smaller than its previous one (if exists).
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int compareBitonicSums(vector<int>& nums) {
int idx = max_element(begin(nums), end(nums)) - begin(nums);
long long sum = 0;
for(int i = 0; i < nums.size(); i++) {
if(i != idx) sum = (sum + (i < idx ? 1 : -1) * nums[i]);
}
return sum > 0 ? 0 : sum < 0 ? 1 : -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/compare-sums-of-bitonic-parts/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.