LeetCode in Kotlin

2270. Number of Ways to Split Array

Medium

You are given a 0-indexed integer array nums of length n.

nums contains a valid split at index i if the following are true:

Return the number of valid splits in nums.

Example 1:

Input: nums = [10,4,-8,7]

Output: 2

Explanation: There are three ways of splitting nums into two non-empty parts:

Example 2:

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

Output: 2

Explanation: There are two valid splits in nums:

Constraints:

Solution

class Solution {
    fun waysToSplitArray(nums: IntArray): Int {
        var leftSum: Long = 0
        var rightSum: Long = 0
        for (i in nums) {
            rightSum += i.toLong()
        }
        var count = 0
        for (i in 0 until nums.size - 1) {
            rightSum -= nums[i].toLong()
            leftSum += nums[i].toLong()
            if (leftSum >= rightSum) {
                count++
            }
        }
        return count
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy