-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharr_split_3_parts_eq_sum.py
More file actions
38 lines (29 loc) · 1.05 KB
/
Copy patharr_split_3_parts_eq_sum.py
File metadata and controls
38 lines (29 loc) · 1.05 KB
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
34
35
36
37
38
'''
Given an integer array, nums, return whether or not you can split the array into
three subarrays all of which have an equal sum.
Ex: Given the following nums…
nums = [2, 3, 9, 9, 3, 2, 3, 2, 9], return true.
Ex: Given the following nums…
nums = [1, 2, 3], return false.
'''
def can_split_three_parts(nums):
total_sum = sum(nums)
# If the total sum is not divisible by 3, it's impossible to split
if total_sum % 3 != 0:
return False
target_sum = total_sum // 3
current_sum = 0
count = 0
for num in nums:
current_sum += num
if current_sum == target_sum:
count += 1
current_sum = 0
# If we've found 2 subarrays, the remaining must be the third
if count == 2:
return True
return False
# Test cases
#print(can_split_three_parts([2, 3, 9, 9, 3, 2, 3, 2, 9])) # Should return True
#print(can_split_three_parts([1, 2, 3])) # Should return False
print(can_split_three_parts([2, 3, 9, 14, 3, 2, 9]))