From 26c468b8790c6a9dae8e5dbe18fd7f2c69d3bba6 Mon Sep 17 00:00:00 2001 From: Divlo Date: Sun, 1 May 2022 18:41:47 +0200 Subject: [PATCH] feat(challenges): add `maximum-subarray-sum` --- challenges/maximum-subarray-sum/README.md | 68 +++++++++++++++++++ .../maximum-subarray-sum/solutions/.gitkeep | 0 .../maximum-subarray-sum/test/1/input.txt | 7 ++ .../maximum-subarray-sum/test/1/output.txt | 1 + .../maximum-subarray-sum/test/2/input.txt | 9 +++ .../maximum-subarray-sum/test/2/output.txt | 1 + .../maximum-subarray-sum/test/3/input.txt | 7 ++ .../maximum-subarray-sum/test/3/output.txt | 1 + .../maximum-subarray-sum/test/4/input.txt | 9 +++ .../maximum-subarray-sum/test/4/output.txt | 1 + .../maximum-subarray-sum/test/5/input.txt | 8 +++ .../maximum-subarray-sum/test/5/output.txt | 1 + 12 files changed, 113 insertions(+) create mode 100644 challenges/maximum-subarray-sum/README.md create mode 100644 challenges/maximum-subarray-sum/solutions/.gitkeep create mode 100644 challenges/maximum-subarray-sum/test/1/input.txt create mode 100644 challenges/maximum-subarray-sum/test/1/output.txt create mode 100644 challenges/maximum-subarray-sum/test/2/input.txt create mode 100644 challenges/maximum-subarray-sum/test/2/output.txt create mode 100644 challenges/maximum-subarray-sum/test/3/input.txt create mode 100644 challenges/maximum-subarray-sum/test/3/output.txt create mode 100644 challenges/maximum-subarray-sum/test/4/input.txt create mode 100644 challenges/maximum-subarray-sum/test/4/output.txt create mode 100644 challenges/maximum-subarray-sum/test/5/input.txt create mode 100644 challenges/maximum-subarray-sum/test/5/output.txt diff --git a/challenges/maximum-subarray-sum/README.md b/challenges/maximum-subarray-sum/README.md new file mode 100644 index 0000000..2aa9914 --- /dev/null +++ b/challenges/maximum-subarray-sum/README.md @@ -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`. diff --git a/challenges/maximum-subarray-sum/solutions/.gitkeep b/challenges/maximum-subarray-sum/solutions/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/challenges/maximum-subarray-sum/test/1/input.txt b/challenges/maximum-subarray-sum/test/1/input.txt new file mode 100644 index 0000000..7d3b5f4 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/1/input.txt @@ -0,0 +1,7 @@ +6 +1 +2 +3 +4 +5 +6 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/1/output.txt b/challenges/maximum-subarray-sum/test/1/output.txt new file mode 100644 index 0000000..b5045cc --- /dev/null +++ b/challenges/maximum-subarray-sum/test/1/output.txt @@ -0,0 +1 @@ +21 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/2/input.txt b/challenges/maximum-subarray-sum/test/2/input.txt new file mode 100644 index 0000000..874c8cf --- /dev/null +++ b/challenges/maximum-subarray-sum/test/2/input.txt @@ -0,0 +1,9 @@ +8 +-1 +2 +4 +-3 +5 +2 +-5 +2 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/2/output.txt b/challenges/maximum-subarray-sum/test/2/output.txt new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/2/output.txt @@ -0,0 +1 @@ +10 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/3/input.txt b/challenges/maximum-subarray-sum/test/3/input.txt new file mode 100644 index 0000000..71a6d27 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/3/input.txt @@ -0,0 +1,7 @@ +6 +-1 +-2 +-3 +-4 +-5 +-6 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/3/output.txt b/challenges/maximum-subarray-sum/test/3/output.txt new file mode 100644 index 0000000..d7d17fc --- /dev/null +++ b/challenges/maximum-subarray-sum/test/3/output.txt @@ -0,0 +1 @@ +-1 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/4/input.txt b/challenges/maximum-subarray-sum/test/4/input.txt new file mode 100644 index 0000000..d83afb4 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/4/input.txt @@ -0,0 +1,9 @@ +8 +-2 +-3 +4 +-1 +-2 +1 +5 +-3 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/4/output.txt b/challenges/maximum-subarray-sum/test/4/output.txt new file mode 100644 index 0000000..c793025 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/4/output.txt @@ -0,0 +1 @@ +7 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/5/input.txt b/challenges/maximum-subarray-sum/test/5/input.txt new file mode 100644 index 0000000..2a62272 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/5/input.txt @@ -0,0 +1,8 @@ +7 +11 +-8 +16 +-7 +24 +-2 +3 \ No newline at end of file diff --git a/challenges/maximum-subarray-sum/test/5/output.txt b/challenges/maximum-subarray-sum/test/5/output.txt new file mode 100644 index 0000000..7c09198 --- /dev/null +++ b/challenges/maximum-subarray-sum/test/5/output.txt @@ -0,0 +1 @@ +37 \ No newline at end of file