Medium
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 = 7Constraints:
3 <= n == nums.length <= 1051 <= nums[i] <= 109nums is a bitonic array.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;
}
}