TheAlgorithms-C/leetcode/src/2270.c
Alexander Pantyukhin 1d07d142d0
feat: add Number of Ways to Split Array LeetCode (#1165)
Co-authored-by: David Leal <halfpacho@gmail.com>
2022-12-16 15:42:57 -06:00

22 lines
503 B
C

// Prefix sum.
// Collect sum fromleft part and compare it with left sum.
// Runtime: O(n)
// Space: O(1)
int waysToSplitArray(int* nums, int numsSize){
long sumNums = 0;
for (int i = 0; i < numsSize; i++){
sumNums += nums[i];
}
long prefixSum = 0;
int result = 0;
for (int i = 0; i < numsSize - 1; i++){
prefixSum += nums[i];
if (prefixSum >= sumNums - prefixSum){
result += 1;
}
}
return result;
}