Number of ways to split array
November 19, 2022
array-and-hashmapProblem URL: Number of ways to split array
We will keep track of the left sum and right sum of the array. Then we iterate over each element until the last element, and we will check if the left sum is equal to the right sum. If yes, we will increment the result by 1. We will continue this process until we reach the end of the array.
class Solution:
def waysToSplitArray(self, nums: List[int]) -> int:
lSum, rSum = 0, sum(nums)
res = 0
for num in nums[:-1]:
lSum += num
rSum -= num
if lSum >= rSum:
res += 1
return res
Time complexity: O(n)
Space complexity: O(1)