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.class Solution {
fun compareBitonicSums(nums: IntArray): Int {
var asc: Long = 0
var desc: Long = 0
var i: Int
i = 0
while (i < nums.size - 1) {
asc += nums[i].toLong()
if (nums[i] > nums[i + 1]) {
break
}
i++
}
while (i < nums.size) {
desc += nums[i].toLong()
i++
}
if (asc == desc) {
return -1
}
return if (asc > desc) 0 else 1
}
}