Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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:
i + 1
elements is greater than or equal to the sum of the last n - i - 1
elements.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
class Solution {
public:
int waysToSplitArray(vector<int>& nums) {
long long sumFromBack(0), sumFromFront(0);
for (auto& i : nums) sumFromBack += i;
int n(size(nums)), res(0);
for (auto i=0; i<n-1; i++) {
sumFromFront += nums[i]; // sum of the first i + 1 elements
sumFromBack -= nums[i]; // sum of the last n - i - 1 elements.
if (sumFromFront >= sumFromBack) res++;
}
return res;
}
};
class Solution:
def waysToSplitArray(self, n: List[int]) -> int:
n = list(accumulate(n))
return sum(n[i] >= n[-1] - n[i] for i in range(len(n) - 1))
class Solution {
public int waysToSplitArray(int[] nums) {
long sum = 0;
for(int num : nums) {
sum += num;
}
long leftSum = 0;
int res = 0;
for(int i = 0; i < nums.length-1; i++) {
leftSum += nums[i];
long rightSum = sum - leftSum;
if(leftSum >= rightSum) {
res++;
}
}
return res;
}
}
In our experience, we suggest you solve this Number of Ways to Split Array and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Number of Ways to Split Array LeetCode Solution
I hope this Number of Ways to Split Array LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>