Skip to content

Commit bcd9cd9

Browse files
Add files via upload
1 parent 6b6dd81 commit bcd9cd9

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 3432. Count Partitions with Even Sum Difference
2+
# Solved
3+
# Easy
4+
# Topics
5+
# premium lock icon
6+
# Companies
7+
# Hint
8+
# You are given an integer array nums of length n.
9+
10+
# A partition is defined as an index i where 0 <= i < n - 1, splitting the array into two non-empty subarrays such that:
11+
12+
# Left subarray contains indices [0, i].
13+
# Right subarray contains indices [i + 1, n - 1].
14+
# Return the number of partitions where the difference between the sum of the left and right subarrays is even.
15+
16+
17+
18+
# Example 1:
19+
20+
# Input: nums = [10,10,3,7,6]
21+
22+
# Output: 4
23+
24+
# Explanation:
25+
26+
# The 4 partitions are:
27+
28+
# [10], [10, 3, 7, 6] with a sum difference of 10 - 26 = -16, which is even.
29+
# [10, 10], [3, 7, 6] with a sum difference of 20 - 16 = 4, which is even.
30+
# [10, 10, 3], [7, 6] with a sum difference of 23 - 13 = 10, which is even.
31+
# [10, 10, 3, 7], [6] with a sum difference of 30 - 6 = 24, which is even.
32+
# Example 2:
33+
34+
# Input: nums = [1,2,2]
35+
36+
# Output: 0
37+
38+
# Explanation:
39+
40+
# No partition results in an even sum difference.
41+
42+
# Example 3:
43+
44+
# Input: nums = [2,4,6,8]
45+
46+
# Output: 3
47+
48+
# Explanation:
49+
50+
# All partitions result in an even sum difference.
51+
52+
53+
54+
# Constraints:
55+
56+
# 2 <= n == nums.length <= 100
57+
# 1 <= nums[i] <= 100
58+
59+
class Solution:
60+
def countPartitions(self, nums: List[int]) -> int:
61+
x = 0
62+
total = sum(nums)
63+
prefix = 0
64+
for i in range(len(nums)-1):
65+
prefix += nums[i]
66+
suffix = total - prefix
67+
y = prefix - suffix
68+
if y % 2 == 0:
69+
x += 1
70+
return x
71+
72+
class solution:
73+
def countPartitions2(self,nums):
74+
if sum(nums) % 2 : return 0
75+
return len(nums) - 1

0 commit comments

Comments
 (0)