2750. Ways to Split Array Into Good Subarrays LeetCode Solution
In this guide, you will get 2750. Ways to Split Array Into Good Subarrays LeetCode Solution with the best time and space complexity. The solution to Ways to Split Array Into Good Subarrays problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Ways to Split Array Into Good Subarrays solution in C++
- Ways to Split Array Into Good Subarrays solution in Java
- Ways to Split Array Into Good Subarrays solution in Python
- Additional Resources

Problem Statement of Ways to Split Array Into Good Subarrays
You are given a binary array nums.
A subarray of an array is good if it contains exactly one element with the value 1.
Return an integer denoting the number of ways to split the array nums into good subarrays. As the number may be too large, return it modulo 109 + 7.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [0,1,0,0,1]
Output: 3
Explanation: There are 3 ways to split nums into good subarrays:
– [0,1] [0,0,1]
– [0,1,0] [0,1]
– [0,1,0,0] [1]
Example 2:
Input: nums = [0,1,0]
Output: 1
Explanation: There is 1 way to split nums into good subarrays:
– [0,1,0]
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 1
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2750. Ways to Split Array Into Good Subarrays LeetCode Solution in C++
class Solution {
public:
int numberOfGoodSubarraySplits(vector<int>& nums) {
if (ranges::count(nums, 1) == 0)
return 0;
constexpr int kMod = 1'000'000'007;
int prev = -1; // the previous index of 1
int ans = 1;
for (int i = 0; i < nums.size(); ++i)
if (nums[i] == 1) {
if (prev != -1)
ans = ans * static_cast<long>(i - prev) % kMod;
prev = i;
}
return ans;
}
};
/* code provided by PROGIEZ */
2750. Ways to Split Array Into Good Subarrays LeetCode Solution in Java
class Solution {
public int numberOfGoodSubarraySplits(int[] nums) {
if (Arrays.stream(nums).filter(num -> num == 1).count() == 0)
return 0;
final int kMod = 1_000_000_007;
int prev = -1; // the previous index of 1
int ans = 1;
for (int i = 0; i < nums.length; ++i)
if (nums[i] == 1) {
if (prev != -1)
ans = (int) ((long) ans * (i - prev) % kMod);
prev = i;
}
return ans;
}
}
// code provided by PROGIEZ
2750. Ways to Split Array Into Good Subarrays LeetCode Solution in Python
class Solution:
def numberOfGoodSubarraySplits(self, nums: list[int]) -> int:
if 1 not in nums:
return 0
kMod = 1_000_000_007
prev = -1 # the previous index of 1
ans = 1
for i, num in enumerate(nums):
if num == 1:
if prev != -1:
ans *= i - prev
ans %= kMod
prev = i
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.