Skip to main content
Back to problems
#3909
Medium Algorithms

Compare sums of bitonic parts

53.1% acceptance
May 13, 2026
21
2
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).

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn compare_bitonic_sums(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let mut peak = 0;
    for i in 1..n {
      if nums[i] > nums[peak] {
        peak = i;
      } else {
        break;
      }
    }
    let asc: i64 = nums[..=peak].iter().map(|&x| x as i64).sum();
    let desc: i64 = nums[peak..].iter().map(|&x| x as i64).sum();
    if asc > desc { 0 } else if desc > asc { 1 } else { -1 }
  }
}