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:
parent
45ae2dcf5c
commit
26c468b879
68
challenges/maximum-subarray-sum/README.md
Normal file
68
challenges/maximum-subarray-sum/README.md
Normal 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`.
|
0
challenges/maximum-subarray-sum/solutions/.gitkeep
Normal file
0
challenges/maximum-subarray-sum/solutions/.gitkeep
Normal file
7
challenges/maximum-subarray-sum/test/1/input.txt
Normal file
7
challenges/maximum-subarray-sum/test/1/input.txt
Normal file
@ -0,0 +1,7 @@
|
||||
6
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
1
challenges/maximum-subarray-sum/test/1/output.txt
Normal file
1
challenges/maximum-subarray-sum/test/1/output.txt
Normal file
@ -0,0 +1 @@
|
||||
21
|
9
challenges/maximum-subarray-sum/test/2/input.txt
Normal file
9
challenges/maximum-subarray-sum/test/2/input.txt
Normal file
@ -0,0 +1,9 @@
|
||||
8
|
||||
-1
|
||||
2
|
||||
4
|
||||
-3
|
||||
5
|
||||
2
|
||||
-5
|
||||
2
|
1
challenges/maximum-subarray-sum/test/2/output.txt
Normal file
1
challenges/maximum-subarray-sum/test/2/output.txt
Normal file
@ -0,0 +1 @@
|
||||
10
|
7
challenges/maximum-subarray-sum/test/3/input.txt
Normal file
7
challenges/maximum-subarray-sum/test/3/input.txt
Normal file
@ -0,0 +1,7 @@
|
||||
6
|
||||
-1
|
||||
-2
|
||||
-3
|
||||
-4
|
||||
-5
|
||||
-6
|
1
challenges/maximum-subarray-sum/test/3/output.txt
Normal file
1
challenges/maximum-subarray-sum/test/3/output.txt
Normal file
@ -0,0 +1 @@
|
||||
-1
|
9
challenges/maximum-subarray-sum/test/4/input.txt
Normal file
9
challenges/maximum-subarray-sum/test/4/input.txt
Normal file
@ -0,0 +1,9 @@
|
||||
8
|
||||
-2
|
||||
-3
|
||||
4
|
||||
-1
|
||||
-2
|
||||
1
|
||||
5
|
||||
-3
|
1
challenges/maximum-subarray-sum/test/4/output.txt
Normal file
1
challenges/maximum-subarray-sum/test/4/output.txt
Normal file
@ -0,0 +1 @@
|
||||
7
|
8
challenges/maximum-subarray-sum/test/5/input.txt
Normal file
8
challenges/maximum-subarray-sum/test/5/input.txt
Normal file
@ -0,0 +1,8 @@
|
||||
7
|
||||
11
|
||||
-8
|
||||
16
|
||||
-7
|
||||
24
|
||||
-2
|
||||
3
|
1
challenges/maximum-subarray-sum/test/5/output.txt
Normal file
1
challenges/maximum-subarray-sum/test/5/output.txt
Normal file
@ -0,0 +1 @@
|
||||
37
|
Loading…
Reference in New Issue
Block a user