|
| 1 | +--- |
| 2 | +id: sum-of-all-odd-length-subarrays |
| 3 | +title: Sum of All Odd Length Subarrays Solution |
| 4 | +sidebar_label: 1588-Sum-of-All-Odd-Length-Subarrays |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Prefix Sum |
| 8 | + - LeetCode |
| 9 | + - Python |
| 10 | + - JavaScript |
| 11 | + - TypeScript |
| 12 | + - Java |
| 13 | + - C++ |
| 14 | +description: "This is a solution to the Sum of All Odd Length Subarrays problem on LeetCode." |
| 15 | +--- |
| 16 | + |
| 17 | +In this page, we will solve the Sum of All Odd Length Subarrays problem using multiple approaches: brute-force and prefix sum optimization. We will provide the implementation of the solution in Python, JavaScript, TypeScript, Java, and C++. |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +Given an array of positive integers `arr`, calculate the sum of all possible odd-length subarrays. |
| 22 | + |
| 23 | +A subarray is a contiguous subsequence of the array. |
| 24 | + |
| 25 | +Return the sum of all odd-length subarrays of `arr`. |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +```plaintext |
| 32 | +Input: arr = [1,4,2,5,3] |
| 33 | +Output: 58 |
| 34 | +Explanation: The odd-length subarrays of `arr` and their sums are: |
| 35 | +- [1] => 1 |
| 36 | +- [4] => 4 |
| 37 | +- [2] => 2 |
| 38 | +- [5] => 5 |
| 39 | +- [3] => 3 |
| 40 | +- [1,4,2] => 7 |
| 41 | +- [4,2,5] => 11 |
| 42 | +- [2,5,3] => 10 |
| 43 | +- [1,4,2,5,3] => 15 |
| 44 | +If we sum everything, we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58. |
| 45 | +``` |
| 46 | + |
| 47 | +**Example 2:** |
| 48 | + |
| 49 | +```plaintext |
| 50 | +Input: arr = [1,2] |
| 51 | +Output: 3 |
| 52 | +Explanation: There are only 2 elements. So, only subarrays of length 1 and 2 will be considered. The sum of them is 1 + 2 = 3. |
| 53 | +``` |
| 54 | + |
| 55 | +**Example 3:** |
| 56 | + |
| 57 | +```plaintext |
| 58 | +Input: arr = [10,11,12] |
| 59 | +Output: 66 |
| 60 | +``` |
| 61 | + |
| 62 | +### Constraints |
| 63 | + |
| 64 | +- $1 <= arr.length <= 100$ |
| 65 | +- $1 <= arr[i] <= 1000$ |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Solution for Sum of All Odd Length Subarrays |
| 70 | + |
| 71 | +### Intuition and Approach |
| 72 | + |
| 73 | +The problem can be solved using different approaches. We will start with a brute-force approach and then look at an optimized approach using prefix sums. |
| 74 | + |
| 75 | +<Tabs> |
| 76 | + <tabItem value="Brute Force" label="Brute Force"> |
| 77 | + |
| 78 | +### Approach 1: Brute Force |
| 79 | + |
| 80 | +We iterate through all possible subarrays of odd lengths and calculate their sums. |
| 81 | + |
| 82 | +#### Implementation |
| 83 | +```jsx live |
| 84 | +function sumOfOddLengthSubarrays() { |
| 85 | + const arr = [1, 4, 2, 5, 3]; |
| 86 | + |
| 87 | + const sumOddLengthSubarrays = function(arr) { |
| 88 | + let result = 0; |
| 89 | + for (let i = 0; i < arr.length; i++) { |
| 90 | + for (let j = i; j < arr.length; j += 2) { |
| 91 | + for (let k = i; k <= j; k++) { |
| 92 | + result += arr[k]; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return result; |
| 97 | + }; |
| 98 | + |
| 99 | + const result = sumOddLengthSubarrays(arr); |
| 100 | + return ( |
| 101 | + <div> |
| 102 | + <p> |
| 103 | + <b>Input:</b> arr = {JSON.stringify(arr)} |
| 104 | + </p> |
| 105 | + <p> |
| 106 | + <b>Output:</b> {result} |
| 107 | + </p> |
| 108 | + </div> |
| 109 | + ); |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +#### Codes in Different Languages |
| 114 | + |
| 115 | +<Tabs> |
| 116 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 117 | + <SolutionAuthor name="@manishh12"/> |
| 118 | + ```javascript |
| 119 | + function sumOddLengthSubarrays(arr) { |
| 120 | + let result = 0; |
| 121 | + for (let i = 0; i < arr.length; i++) { |
| 122 | + for (let j = i; j < arr.length; j += 2) { |
| 123 | + for (let k = i; k <= j; k++) { |
| 124 | + result += arr[k]; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return result; |
| 129 | + } |
| 130 | + ``` |
| 131 | + |
| 132 | + </TabItem> |
| 133 | + <TabItem value="TypeScript" label="TypeScript"> |
| 134 | + <SolutionAuthor name="@manishh12"/> |
| 135 | + ```typescript |
| 136 | + function sumOddLengthSubarrays(arr: number[]): number { |
| 137 | + let result = 0; |
| 138 | + for (let i = 0; i < arr.length; i++) { |
| 139 | + for (let j = i; j < arr.length; j += 2) { |
| 140 | + for (let k = i; k <= j; k++) { |
| 141 | + result += arr[k]; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + return result; |
| 146 | + } |
| 147 | + ``` |
| 148 | + |
| 149 | + </TabItem> |
| 150 | + <TabItem value="Python" label="Python"> |
| 151 | + <SolutionAuthor name="@manishh12"/> |
| 152 | + ```python |
| 153 | + class Solution: |
| 154 | + def sumOddLengthSubarrays(self, arr: List[int]) -> int: |
| 155 | + result = 0 |
| 156 | + for i in range(len(arr)): |
| 157 | + for j in range(i, len(arr), 2): |
| 158 | + result += sum(arr[i:j+1]) |
| 159 | + return result |
| 160 | + ``` |
| 161 | + |
| 162 | + </TabItem> |
| 163 | + <TabItem value="Java" label="Java"> |
| 164 | + <SolutionAuthor name="@manishh12"/> |
| 165 | + ```java |
| 166 | + class Solution { |
| 167 | + public int sumOddLengthSubarrays(int[] arr) { |
| 168 | + int result = 0; |
| 169 | + for (int i = 0; i < arr.length; i++) { |
| 170 | + for (int j = i; j < arr.length; j += 2) { |
| 171 | + for (int k = i; k <= j; k++) { |
| 172 | + result += arr[k]; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + return result; |
| 177 | + } |
| 178 | + } |
| 179 | + ``` |
| 180 | + |
| 181 | + </TabItem> |
| 182 | + <TabItem value="C++" label="C++"> |
| 183 | + <SolutionAuthor name="@manishh12"/> |
| 184 | + ```cpp |
| 185 | + class Solution { |
| 186 | + public: |
| 187 | + int sumOddLengthSubarrays(vector<int>& arr) { |
| 188 | + int result = 0; |
| 189 | + for (int i = 0; i < arr.size(); i++) { |
| 190 | + for (int j = i; j < arr.size(); j += 2) { |
| 191 | + for (int k = i; k <= j; k++) { |
| 192 | + result += arr[k]; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + return result; |
| 197 | + } |
| 198 | + }; |
| 199 | + ``` |
| 200 | + |
| 201 | + </TabItem> |
| 202 | +</Tabs> |
| 203 | + |
| 204 | +#### Complexity Analysis |
| 205 | + |
| 206 | +- Time Complexity: $$O(n^3)$$ |
| 207 | +- Space Complexity: $$O(1)$$ |
| 208 | +- Where `n` is the length of the array `arr`. |
| 209 | +- The time complexity is cubic due to three nested loops iterating through the array. |
| 210 | +- The space complexity is constant as no additional space is required beyond a few variables. |
| 211 | + |
| 212 | +</tabItem> |
| 213 | +<tabItem value="Prefix Sum" label="Prefix Sum"> |
| 214 | + |
| 215 | +### Approach 2: Prefix Sum |
| 216 | + |
| 217 | +We can optimize the brute-force solution by precomputing prefix sums. This allows us to calculate the sum of any subarray in constant time. |
| 218 | + |
| 219 | +#### Implementation |
| 220 | + |
| 221 | +```jsx live |
| 222 | +function sumOfOddLengthSubarrays() { |
| 223 | + const arr = [1, 4, 2, 5, 3]; |
| 224 | +
|
| 225 | + const sumOddLengthSubarrays = function(arr) { |
| 226 | + const prefixSum = [0]; |
| 227 | + for (let num of arr) { |
| 228 | + prefixSum.push(prefixSum[prefixSum.length - 1] + num); |
| 229 | + } |
| 230 | +
|
| 231 | + let result = 0; |
| 232 | + for (let i = 0; i < arr.length; i++) { |
| 233 | + for (let j = i; j < arr.length; j += 2) { |
| 234 | + result += prefixSum[j + 1] - prefixSum[i]; |
| 235 | + } |
| 236 | + } |
| 237 | +
|
| 238 | + return result; |
| 239 | + }; |
| 240 | +
|
| 241 | + const result = sumOddLengthSubarrays(arr); |
| 242 | + return ( |
| 243 | + <div> |
| 244 | + <p> |
| 245 | + <b>Input:</b> arr = {JSON.stringify(arr)} |
| 246 | + </p> |
| 247 | + <p> |
| 248 | + <b>Output:</b> {result} |
| 249 | + </p> |
| 250 | + </div> |
| 251 | + ); |
| 252 | +} |
| 253 | +``` |
| 254 | + |
| 255 | +#### Codes in Different Languages |
| 256 | + |
| 257 | +<Tabs> |
| 258 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 259 | + <SolutionAuthor name="@manishh12"/> |
| 260 | + ```javascript |
| 261 | + function sumOddLengthSubarrays(arr) { |
| 262 | + const prefixSum = [0]; |
| 263 | + for (let num of arr) { |
| 264 | + prefixSum.push(prefixSum[prefixSum.length - 1] + num); |
| 265 | + } |
| 266 | +
|
| 267 | + let result = 0; |
| 268 | + for (let i = 0; i < arr.length; i++) { |
| 269 | + for (let j = i; j < arr.length; j += 2) { |
| 270 | + result += prefixSum[j + 1] - prefixSum[i]; |
| 271 | + } |
| 272 | + } |
| 273 | +
|
| 274 | + return result; |
| 275 | + } |
| 276 | + ``` |
| 277 | + |
| 278 | + </TabItem> |
| 279 | + <TabItem value="TypeScript" label="TypeScript"> |
| 280 | + <SolutionAuthor name="@manishh12"/> |
| 281 | + ```typescript |
| 282 | + function sumOddLengthSubarrays(arr: number[]): number { |
| 283 | + const prefixSum = [0]; |
| 284 | + for (let num of arr) { |
| 285 | + prefixSum.push(prefixSum[prefixSum.length - 1] + num); |
| 286 | + } |
| 287 | +
|
| 288 | + let result = 0; |
| 289 | + for (let i = 0; i < arr.length; i++) { |
| 290 | + for (let j = i; j < arr.length; j += 2) { |
| 291 | + result += prefixSum[j + 1] - prefixSum[i]; |
| 292 | + } |
| 293 | + } |
| 294 | +
|
| 295 | + return result; |
| 296 | + } |
| 297 | + ``` |
| 298 | + |
| 299 | + </TabItem> |
| 300 | + <TabItem value="Python" label="Python"> |
| 301 | + <SolutionAuthor name="@manishh12"/> |
| 302 | + ```python |
| 303 | + class Solution: |
| 304 | + def sumOddLengthSubarrays(self, arr: List[int]) -> int: |
| 305 | + prefix_sum = [0] |
| 306 | + for num in arr: |
| 307 | + prefix_sum.append(prefix_sum[-1] + num) |
| 308 | +
|
| 309 | + result = 0 |
| 310 | + for i in range(len(arr)): |
| 311 | + for j in range(i, len(arr), 2): |
| 312 | + result += prefix_sum[j + 1] - prefix_sum[i] |
| 313 | +
|
| 314 | + return result |
| 315 | + ``` |
| 316 | + |
| 317 | + </TabItem> |
| 318 | + <TabItem value="Java" label="Java"> |
| 319 | + <SolutionAuthor name="@manishh12"/> |
| 320 | + ```java |
| 321 | + class Solution { |
| 322 | + public int sumOddLengthSubarrays(int[] arr) { |
| 323 | + int[] prefixSum = new int[arr.length + 1]; |
| 324 | + for (int i = 0; i < arr.length; i++) { |
| 325 | + prefixSum[i + 1] = prefixSum[i] + arr[i]; |
| 326 | + } |
| 327 | +
|
| 328 | + int result = 0; |
| 329 | + for (int i = 0; i < arr.length; i++) { |
| 330 | + for (int j = i; j < arr.length; j += 2) { |
| 331 | + result += prefixSum[j + 1] - prefixSum[i]; |
| 332 | + } |
| 333 | + } |
| 334 | +
|
| 335 | + return result; |
| 336 | + } |
| 337 | + } |
| 338 | + ``` |
| 339 | + |
| 340 | + </TabItem> |
| 341 | + <TabItem value="C++" label="C++"> |
| 342 | + <SolutionAuthor name="@manishh12"/> |
| 343 | + ```cpp |
| 344 | + class Solution { |
| 345 | + public: |
| 346 | + int sumOddLengthSubarrays(vector<int>& arr) { |
| 347 | + vector<int> prefixSum(arr.size() + 1, 0); |
| 348 | + for (int i = 0; i < arr.size(); i++) { |
| 349 | + prefixSum[i + 1] = prefixSum[i] + arr[i]; |
| 350 | + } |
| 351 | +
|
| 352 | + int result = 0; |
| 353 | + for (int i = 0; i < arr.size(); i++) { |
| 354 | + for (int j = i; j < arr.size(); j += 2) { |
| 355 | + result += prefixSum[j + 1] - prefixSum[i]; |
| 356 | + } |
| 357 | + } |
| 358 | +
|
| 359 | + return result; |
| 360 | + } |
| 361 | + }; |
| 362 | + ``` |
| 363 | + |
| 364 | + </TabItem> |
| 365 | +</Tabs> |
| 366 | + |
| 367 | +#### Complexity Analysis |
| 368 | + |
| 369 | +- Time Complexity: $$O(n^2)$$ |
| 370 | +- Space Complexity: $$O(n)$$ |
| 371 | +- Where `n` is the length of the array `arr`. |
| 372 | +- The time complexity is quadratic due to two nested loops iterating through the array. |
| 373 | +- The space complexity is linear as we use extra space to store prefix sums. |
| 374 | + |
| 375 | +</tabItem> |
| 376 | +</Tabs> |
| 377 | + |
| 378 | +:::tip Note |
| 379 | + |
| 380 | +By using these approaches, we can efficiently solve the Sum of All Odd Length Subarrays problem for the given constraints. |
| 381 | + |
| 382 | +::: |
| 383 | +## References |
| 384 | + |
| 385 | +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/sum-of-all-odd-length-subarrays/) |
| 386 | +- **Solution Link:** [Sum of All Odd Length Subarrays Solution on LeetCode](https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/851730/Brute-Force-and-Prefix-Sum-Solutions-in-Python-Java-JavaScript-C%2B%2B) |
| 387 | +- **Authors LeetCode Profile:** [Manish Kumar Gupta](https://leetcode.com/_manishh12/) |
| 388 | + |
0 commit comments