First, there's a grammatical error in the description. It should say "continuous" instead of "contiguous".

Say you have a sequence [2, 3, -4].

Let's create all possible continuous subarrays of this sequence. These are: [2],[2, 3],[2, 3, -4],[3],[3, -4],[-4].
Which one of these sums to the greatest value? If you sum the elements in each subarray and make a comparison, you'll see that [2, 3] is the answer.

Now, let's make the sequence [2, 3, -4, 6]. Again, let's create all possible continuous subarrays: [2],[2, 3],[2, 3, -4],[2, 3, -4, 6],[3],[3, -4],[3, -4, 6],[-4],[-4, 6],[6]. Which one of these subarrays sums to the greatest value? Using the same process as above, the answer is [2, 3, -4, 6].

In the example provided by the author, the sequence is [-2, 1, -3, 4, -1, 2, 1, -5, 4] and the output is [4, -1, 2, 1]. That's because if you create all posisble continous subarrays, take the sum of each and compare them, the one with the greatest value is [4, -1, 2, 1].

Using the approach above in your code is, as you might suspect, extremely inefficient and won't work for large sequences. Instead, it's just my attempt to explain it.

should work on the example

Log

[-2, 1, -3, 5, -1, 2, 1, -5, 4]

7 should equal 6

hello can i know why it should be 7 if [ 5, -1, 2, 1] is 7

already done

corrected

corrected

"contiguous subsequence" is not a mistake, it's a shortcut to say all elements of the subsequence are contiguous in the sequence.

and since no-one wants to guess at indentation, use proper markdown formatting

Hi, don't post code without the spoiler tag or everyone can see it (I have added the tag).

This comment is hidden because it contains spoiler information about the solution

Sorry my bad. I thought we have to return the subarray with maximum sum.

If two subarrays have the same maximum then obviously they have to be equal, you can just return any of them.

(Atleast give one thought before raising issue.)

What if there are two subarrays that have the same maximum sum?

This comment is hidden because it contains spoiler information about the solution

Needs a test case for array of length 1

it's asking you to find a continuous sub array (eg: index 0 through 4, 2 through 4, etc) that has the highest sum.

First, there's a grammatical error in the description. It should say "continuous" instead of "contiguous".

Say you have a sequence

`[2, 3, -4]`

.Let's create all possible continuous subarrays of this sequence. These are:

`[2]`

,`[2, 3]`

,`[2, 3, -4]`

,`[3]`

,`[3, -4]`

,`[-4]`

.Which one of these sums to the greatest value? If you sum the elements in each subarray and make a comparison, you'll see that

`[2, 3]`

is the answer.Now, let's make the sequence

`[2, 3, -4, 6]`

. Again, let's create all possible continuous subarrays:`[2]`

,`[2, 3]`

,`[2, 3, -4]`

,`[2, 3, -4, 6]`

,`[3]`

,`[3, -4]`

,`[3, -4, 6]`

,`[-4]`

,`[-4, 6]`

,`[6]`

. Which one of these subarrays sums to the greatest value? Using the same process as above, the answer is`[2, 3, -4, 6]`

.In the example provided by the author, the sequence is

`[-2, 1, -3, 4, -1, 2, 1, -5, 4]`

and the output is`[4, -1, 2, 1]`

. That's because if you create all posisble continous subarrays, take the sum of each and compare them, the one with the greatest value is`[4, -1, 2, 1]`

.Using the approach above in your code is, as you might suspect, extremely inefficient and won't work for large sequences. Instead, it's just my attempt to explain it.

## Loading more items...