aboutsummaryrefslogtreecommitdiff
path: root/num_ways_split_arr.py
blob: 28cf75899bb48d2d6defa495dfb40f452c29cd18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 2270. Number of Ways to Split Array

"""
you are given a 0-indexed integer array 'nums' of lenght 'n'. 'nums' contains
a valid split at index 'i' if the following are true: the sum of the first 'i
+ 1' elements is greater than or equal to the sum of the last 'n - i - 1'
elements, and there is at least one element to the right of 'i'. return the
number of valid splits in 'nums'.
"""


class Solution(object):
    def waysToSplitArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        end, start = 0, 0
        for i in range(0, len(nums)):
            end += nums[i]
        ans = 0
        for i in range(0, len(nums) - 1):
            start += nums[i]
            end -= nums[i]
            if start >= end:
                ans += 1
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.waysToSplitArray(nums=[10, 4, -8, 7]))
    print(obj.waysToSplitArray(nums=[2, 3, 1, 0]))