LeetCode in Kotlin

3028. Ant on the Boundary

Easy

An ant is on a boundary. It sometimes goes left and sometimes right.

You are given an array of non-zero integers nums. The ant starts reading nums from the first element of it to its end. At each step, it moves according to the value of the current element:

Return the number of times the ant returns to the boundary.

Notes:

Example 1:

Input: nums = [2,3,-5]

Output: 1

Explanation: After the first step, the ant is 2 steps to the right of the boundary.

After the second step, the ant is 5 steps to the right of the boundary.

After the third step, the ant is on the boundary. So the answer is 1.

Example 2:

Input: nums = [3,2,-3,-4]

Output: 0

Explanation: After the first step, the ant is 3 steps to the right of the boundary.

After the second step, the ant is 5 steps to the right of the boundary.

After the third step, the ant is 2 steps to the right of the boundary.

After the fourth step, the ant is 2 steps to the left of the boundary.

The ant never returned to the boundary, so the answer is 0.

Constraints:

Solution

class Solution {
    fun returnToBoundaryCount(nums: IntArray): Int {
        var diff: Long = 0
        var res = 0
        for (n in nums) {
            diff += n.toLong()
            // since n is non-zero, so prev diff is not zero,
            // when current diff is zero.
            if (diff == 0L) res++
        }
        return res
    }
}