|
| 1 | +--- |
| 2 | +id: Number-of-Ways-to-Split-Array |
| 3 | +title: Number of Ways to Split Array |
| 4 | +sidebar_label: 2270-Number of Ways to Split Array |
| 5 | +tags: |
| 6 | + - Arrays |
| 7 | + - Brute Force |
| 8 | + - Optimized approach |
| 9 | + - LeetCode |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - C++ |
| 13 | + |
| 14 | +description: "This is a solution to Number of Ways to Split Array problem on LeetCode." |
| 15 | +sidebar_position: 71 |
| 16 | +--- |
| 17 | + |
| 18 | +## Problem Statement |
| 19 | +In this tutorial, we will solve the Number of Ways to Split Array problem . We will provide the implementation of the solution in Python, Java, and C++. |
| 20 | + |
| 21 | +### Problem Description |
| 22 | + |
| 23 | +You are given a 0-indexed integer array nums of length n. |
| 24 | + |
| 25 | +nums contains a valid split at index i if the following are true: |
| 26 | + |
| 27 | +The sum of the first i + 1 elements is greater than or equal to the sum of the last n - i - 1 elements. |
| 28 | +There is at least one element to the right of i. That is, 0 <= i < n - 1. |
| 29 | +Return the number of valid splits in nums. |
| 30 | + |
| 31 | +### Examples |
| 32 | + |
| 33 | +**Example 1:** |
| 34 | +Input: nums = [10,4,-8,7] |
| 35 | +Output: 2 |
| 36 | +Explanation: |
| 37 | +There are three ways of splitting nums into two non-empty parts: |
| 38 | +- 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. |
| 39 | +- 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. |
| 40 | +- 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. |
| 41 | +Thus, the number of valid splits in nums is 2. |
| 42 | +**Example 2:** |
| 43 | +Input: nums = [2,3,1,0] |
| 44 | +Output: 2 |
| 45 | +Explanation: |
| 46 | +There are two valid splits in nums: |
| 47 | +- 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. |
| 48 | +- 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. |
| 49 | + |
| 50 | +### Constraints |
| 51 | +- `2 <= nums.length <= 105` |
| 52 | +- `-105 <= nums[i] <= 105` |
| 53 | +## Solution of Given Problem |
| 54 | + |
| 55 | +### Intuition and Approach |
| 56 | + |
| 57 | +The problem can be solved using a brute force approach or an optimized Technique. |
| 58 | + |
| 59 | +<Tabs> |
| 60 | +<tabItem value="Brute Force" label="Brute Force"> |
| 61 | + |
| 62 | +### Approach 1:Brute Force (Naive) |
| 63 | + |
| 64 | + |
| 65 | +Brute Force Approach: In the brute force approach, we will iterate through all possible split points and check if the condition of a valid split is satisfied. |
| 66 | +#### Codes in Different Languages |
| 67 | + |
| 68 | +<Tabs> |
| 69 | +<TabItem value="C++" label="C++" default> |
| 70 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 71 | + |
| 72 | +```cpp |
| 73 | +#include <vector> |
| 74 | +#include <numeric> |
| 75 | +#include <iostream> |
| 76 | + |
| 77 | +int validSplitsBruteForce(const std::vector<int>& nums) { |
| 78 | + int n = nums.size(); |
| 79 | + int count = 0; |
| 80 | + for (int i = 0; i < n - 1; ++i) { |
| 81 | + int leftSum = std::accumulate(nums.begin(), nums.begin() + i + 1, 0); |
| 82 | + int rightSum = std::accumulate(nums.begin() + i + 1, nums.end(), 0); |
| 83 | + if (leftSum >= rightSum) { |
| 84 | + count++; |
| 85 | + } |
| 86 | + } |
| 87 | + return count; |
| 88 | +} |
| 89 | + |
| 90 | +int main() { |
| 91 | + std::vector<int> nums = {10, 4, -8, 7}; |
| 92 | + std::cout << "Number of valid splits: " << validSplitsBruteForce(nums) << std::endl; |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +``` |
| 98 | +</TabItem> |
| 99 | +<TabItem value="Java" label="Java"> |
| 100 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 101 | +
|
| 102 | +```java |
| 103 | +import java.util.*; |
| 104 | +
|
| 105 | +public class Main { |
| 106 | + public static int validSplitsBruteForce(int[] nums) { |
| 107 | + int n = nums.length; |
| 108 | + int count = 0; |
| 109 | + for (int i = 0; i < n - 1; ++i) { |
| 110 | + int leftSum = 0, rightSum = 0; |
| 111 | + for (int j = 0; j <= i; ++j) { |
| 112 | + leftSum += nums[j]; |
| 113 | + } |
| 114 | + for (int j = i + 1; j < n; ++j) { |
| 115 | + rightSum += nums[j]; |
| 116 | + } |
| 117 | + if (leftSum >= rightSum) { |
| 118 | + count++; |
| 119 | + } |
| 120 | + } |
| 121 | + return count; |
| 122 | + } |
| 123 | +
|
| 124 | + public static void main(String[] args) { |
| 125 | + int[] nums = {10, 4, -8, 7}; |
| 126 | + System.out.println("Number of valid splits: " + validSplitsBruteForce(nums)); |
| 127 | + } |
| 128 | +} |
| 129 | +
|
| 130 | +
|
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | +</TabItem> |
| 135 | +<TabItem value="Python" label="Python"> |
| 136 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 137 | + |
| 138 | +```python |
| 139 | +def valid_splits_brute_force(nums): |
| 140 | + n = len(nums) |
| 141 | + count = 0 |
| 142 | + for i in range(n - 1): |
| 143 | + left_sum = sum(nums[:i + 1]) |
| 144 | + right_sum = sum(nums[i + 1:]) |
| 145 | + if left_sum >= right_sum: |
| 146 | + count += 1 |
| 147 | + return count |
| 148 | + |
| 149 | +nums = [10, 4, -8, 7] |
| 150 | +print("Number of valid splits:", valid_splits_brute_force(nums)) |
| 151 | + |
| 152 | + |
| 153 | +``` |
| 154 | + |
| 155 | +</TabItem> |
| 156 | +</Tabs> |
| 157 | + |
| 158 | + |
| 159 | +### Complexity Analysis |
| 160 | + |
| 161 | +- Time Complexity: $O(n^2)$ |
| 162 | +- because for each split point, we calculate the sum of the left and right parts independently, leading to nested iterations. |
| 163 | +- Space Complexity: $O(1)$ |
| 164 | +- as no extra space is used other than a few variables for counting and summing. |
| 165 | + |
| 166 | +</tabItem> |
| 167 | +<tabItem value="Optimized approach" label="Optimized approach"> |
| 168 | + |
| 169 | +### Approach 2: Optimized approach |
| 170 | + |
| 171 | +Optimized Approach: In the optimized approach, we will use prefix sums to avoid recalculating the sum of elements multiple times. |
| 172 | + |
| 173 | +#### Code in Different Languages |
| 174 | + |
| 175 | +<Tabs> |
| 176 | +<TabItem value="C++" label="C++" default> |
| 177 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 178 | + |
| 179 | +```cpp |
| 180 | +#include <vector> |
| 181 | +#include <iostream> |
| 182 | + |
| 183 | +int validSplitsOptimized(const std::vector<int>& nums) { |
| 184 | + int n = nums.size(); |
| 185 | + int totalSum = 0; |
| 186 | + for (int num : nums) { |
| 187 | + totalSum += num; |
| 188 | + } |
| 189 | + |
| 190 | + int leftSum = 0, count = 0; |
| 191 | + for (int i = 0; i < n - 1; ++i) { |
| 192 | + leftSum += nums[i]; |
| 193 | + if (leftSum >= totalSum - leftSum) { |
| 194 | + count++; |
| 195 | + } |
| 196 | + } |
| 197 | + return count; |
| 198 | +} |
| 199 | + |
| 200 | +int main() { |
| 201 | + std::vector<int> nums = {10, 4, -8, 7}; |
| 202 | + std::cout << "Number of valid splits: " << validSplitsOptimized(nums) << std::endl; |
| 203 | + return 0; |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +``` |
| 209 | +</TabItem> |
| 210 | +<TabItem value="Java" label="Java"> |
| 211 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 212 | +
|
| 213 | +```java |
| 214 | +public class Main { |
| 215 | + public static int validSplitsOptimized(int[] nums) { |
| 216 | + int n = nums.length; |
| 217 | + int totalSum = 0; |
| 218 | + for (int num : nums) { |
| 219 | + totalSum += num; |
| 220 | + } |
| 221 | +
|
| 222 | + int leftSum = 0, count = 0; |
| 223 | + for (int i = 0; i < n - 1; ++i) { |
| 224 | + leftSum += nums[i]; |
| 225 | + if (leftSum >= totalSum - leftSum) { |
| 226 | + count++; |
| 227 | + } |
| 228 | + } |
| 229 | + return count; |
| 230 | + } |
| 231 | +
|
| 232 | + public static void main(String[] args) { |
| 233 | + int[] nums = {10, 4, -8, 7}; |
| 234 | + System.out.println("Number of valid splits: " + validSplitsOptimized(nums)); |
| 235 | + } |
| 236 | +} |
| 237 | +
|
| 238 | +
|
| 239 | +``` |
| 240 | + |
| 241 | + |
| 242 | +</TabItem> |
| 243 | +<TabItem value="Python" label="Python"> |
| 244 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 245 | + |
| 246 | +```python |
| 247 | +def valid_splits_optimized(nums): |
| 248 | + n = len(nums) |
| 249 | + total_sum = sum(nums) |
| 250 | + left_sum = 0 |
| 251 | + count = 0 |
| 252 | + for i in range(n - 1): |
| 253 | + left_sum += nums[i] |
| 254 | + if left_sum >= total_sum - left_sum: |
| 255 | + count += 1 |
| 256 | + return count |
| 257 | + |
| 258 | +nums = [10, 4, -8, 7] |
| 259 | +print("Number of valid splits:", valid_splits_optimized(nums)) |
| 260 | + |
| 261 | + |
| 262 | +``` |
| 263 | + |
| 264 | +</TabItem> |
| 265 | +</Tabs> |
| 266 | + |
| 267 | +#### Complexity Analysis |
| 268 | + |
| 269 | +- Time Complexity: $O(n)$ |
| 270 | +- because we compute the total sum once and then iterate through the array once to update the left sum and check the condition. |
| 271 | +- Space Complexity: $O(1)$ |
| 272 | +- as only a few variables are used for summing and counting. |
| 273 | +- This approach is efficient and straightforward. |
| 274 | + |
| 275 | +</tabItem> |
| 276 | +</Tabs> |
| 277 | + |
| 278 | +--- |
| 279 | + |
| 280 | +<h2>Authors:</h2> |
| 281 | + |
| 282 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 283 | +{['AmruthaPariprolu'].map(username => ( |
| 284 | + <Author key={username} username={username} /> |
| 285 | +))} |
| 286 | +</div> |
0 commit comments