1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(challenges): add maximum-subarray-sum

This commit is contained in:
Divlo 2022-05-01 18:41:47 +02:00
parent 45ae2dcf5c
commit 26c468b879
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
12 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,68 @@
# maximum-subarray-sum
Created by [@Divlo](https://github.com/Divlo) on 1 May 2022.
## Instructions
Given an array of `n` integers, find the contiguous subarray with the largest sum.
Contiguous subarray is any sub series of elements in a given array that are contiguous ie their indices are continuous. The problem is interesting when there may be negative values in the array, because if the array only contains positive values, the maximum subarray sum is basically the sum of the array (the subarray being the complete array).
## Input
- **Line 1:** An integer `n` for the length of the list of integers
- **`n` next lines:** the integers
## Output
The largest sum of a contiguous subarray.
## Examples
See the `test` folder for examples of input/output.
### Example 1
#### Input
```txt
6
1
2
3
4
5
6
```
#### Output
```txt
21
```
**Explanation:** The subarray with the largest sum is the array itself (as there is no negative values) `[1, 2, 3, 4, 5, 6]` which has a sum of `21`.
### Example 2
#### Input
```txt
8
-1
2
4
-3
5
2
-5
2
```
#### Output
```txt
10
```
**Explanation:** The subarray with the largest sum is `[2, 4, -3, 5, 2]` which has a sum of `10`.

View File

@ -0,0 +1,7 @@
6
1
2
3
4
5
6

View File

@ -0,0 +1 @@
21

View File

@ -0,0 +1,9 @@
8
-1
2
4
-3
5
2
-5
2

View File

@ -0,0 +1 @@
10

View File

@ -0,0 +1,7 @@
6
-1
-2
-3
-4
-5
-6

View File

@ -0,0 +1 @@
-1

View File

@ -0,0 +1,9 @@
8
-2
-3
4
-1
-2
1
5
-3

View File

@ -0,0 +1 @@
7

View File

@ -0,0 +1,8 @@
7
11
-8
16
-7
24
-2
3

View File

@ -0,0 +1 @@
37