You are given a 0-indexed integer array nums of length n.
nums contains a valid split at index i if the following are true:
- The sum of the first
i + 1elements is greater than or equal to the sum of the lastn - i - 1elements. - There is at least one element to the right of
i. That is,0 <= i < n - 1.
Return the number of valid splits in nums.
Example 1:
Input: nums = [10,4,-8,7] Output: 2 Explanation: There are three ways of splitting nums into two non-empty parts: - Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split. - Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split. - Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split. Thus, the number of valid splits in nums is 2.
Example 2:
Input: nums = [2,3,1,0] Output: 2 Explanation: There are two valid splits in nums: - Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split. - Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split.
Constraints:
2 <= nums.length <= 105-105 <= nums[i] <= 105
Approach 01:
-
C++
-
Python
#include <vector>
using namespace std;
class Solution {
public:
int waysToSplitArray(vector<int>& nums) {
int n = nums.size();
vector<long long> prefixSum(n + 1, 0);
// Compute the prefix sum where prefixSum[i] holds the sum of elements from nums[0] to nums[i-1]
for (int i = 0; i < n; ++i) {
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
long long totalSum = prefixSum[n];
long long validSplitCount = 0;
// Check each position for a valid split
for (int i = 0; i < n - 1; i++) {
if (prefixSum[i + 1] >= (totalSum - prefixSum[i + 1])) {
++validSplitCount;
}
}
return validSplitCount;
}
};
from typing import List
class Solution:
def waysToSplitArray(self, nums: List[int]) -> int:
n = len(nums)
prefixSum = [0] * (n + 1)
# Compute the prefix sum where prefixSum[i] holds the sum of elements from nums[0] to nums[i-1]
for i in range(n):
prefixSum[i + 1] = prefixSum[i] + nums[i]
totalSum = prefixSum[n]
validSplitCount = 0
# Check each position for a valid split
for i in range(n - 1):
if prefixSum[i + 1] >= (totalSum - prefixSum[i + 1]):
validSplitCount += 1
return validSplitCount
Time Complexity:
- Prefix Sum Calculation:
- The prefix sum is calculated by iterating over the array once. This step has a time complexity of \( O(n) \), where \( n \) is the size of the input array nums.
- Valid Split Check:
- The valid split check involves iterating through the array once again (up to \( n – 1 \) positions), which takes \( O(n) \) time.
- Overall Time Complexity:
\( O(n) \), where \( n \) is the size of the input array nums.
Space Complexity:
- Prefix Sum Array:
- The space complexity is dominated by the prefixSum array, which stores cumulative sums and has a size of \( O(n) \).
- Auxiliary Variables:
- The auxiliary variables totalSum and validSplitCount require \( O(1) \) space.
- Overall Space Complexity:
\( O(n) \), due to the space required for the prefix sum array.