Partition Array Into Three Parts With Equal Sum

Easy problem with easy solution.

class Solution {
    fun canThreePartsEqualSum(A: IntArray): Boolean {
        val target = A.sum() / 3
        
        var current = 0
        A.forEach {
            current += it
            if(current == target){
                current = 0
            }
        }
        
        return current == 0        
    }
}
Runtime: 352 ms, faster than 25.00% of Kotlin online submissions for Partition Array Into Three Parts With Equal Sum.
Memory Usage: 50.4 MB, less than 100.00% of Kotlin online submissions for Partition Array Into Three Parts With Equal Sum.

The main idea is to get the total sum and divide it by 3. This will be the target value for each partition. Then we go through each data and calculate a cumulative sum. If the sum equals the target, then we reset the cumulative sum back to zero and start counting again. At the end, if the array can be partitioned equally, then the cumulative sum should be equal to zero.