LeetCode in Kotlin

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

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
    }
}