Skip to content

Commit 93d4b68

Browse files
authored
Create number-of-ways-to-split-array.cpp
1 parent be33ea2 commit 93d4b68

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/number-of-ways-to-split-array.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// prefix sum
5+
class Solution {
6+
public:
7+
int waysToSplitArray(vector<int>& nums) {
8+
int64_t total = accumulate(cbegin(nums), cend(nums), 0ll), curr = 0;
9+
int result = 0;
10+
for (int i = 0; i + 1 < size(nums); ++i) {
11+
curr += nums[i];
12+
result += static_cast<int>(curr >= (total - curr));
13+
}
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)