LeetCode-in-Java

3909. Compare Sums of Bitonic Parts

Medium

You are given a bitonic array nums of length n.

Split the array into two parts:

The peak element belongs to both parts.

Return:

Notes:

Example 1:

Input: nums = [1,3,2,1]

Output: 1

Explanation:

Example 2:

Input: nums = [2,4,5,2]

Output: 0

Explanation:

Example 3:

Input: nums = [1,2,4,3]

Output: -1

Explanation:

Constraints:

Solution

public class Solution {
    public int compareBitonicSums(int[] nums) {
        long asc = 0;
        long desc = 0;
        int i;
        for (i = 0; i < nums.length - 1; i++) {
            asc += nums[i];
            if (nums[i] > nums[i + 1]) {
                break;
            }
        }
        for (; i < nums.length; i++) {
            desc += nums[i];
        }
        if (asc == desc) {
            return -1;
        }
        return asc > desc ? 0 : 1;
    }
}