# Find the sub-array with the largest sum. [ALGORITHM PUZZLE Technical]

1,471.7K Views

You are given an array with integers (both positive and negative) in any random order.

Find the sub-array with the largest sum.

SherlockHolmes Expert Asked on 22nd October 2015 in

Assuming neither the array nor sub-array can be empty, there are two possibilities.

1. Either all values in the array are negative, in which case the sub-array with the largest sum is the 1-element sub-array containing the maximum value, or
2. There are non-negative values in the array and the sub-array is bounded by negative value(s) and/or the maximum/minimum indexes.

The algorithm is to iterate through the array calculating the sum.  Whenever the sum drops below zero, reset the sum to zero and reset the low index.  Whenever the sum exceeds the maximum sum, record the new maximum and the sub-array indexes for the maximum.

Given an array a of size s:

```msum = smallest negative integer
mlow = -1
mhigh = -1
low = -1
sum = 0
for i = 0; i < s; i = i + 1 {
if a[i] < 0 {
if a[i] > msum {
msum = a[i]
mlow = mhigh = i
}
else {
sum += a[i]
if sum < 0 {
sum = 0
low = -1
}
}
}
else {
if low == -1 {
low = i
}
sum += a[i]
if sum > msum {
mlow = low
msum = sum
mhigh = i
}
}
}
// sub-array is a[mlow..mhigh] with a sum of msum
```

dougbell Genius Answered on 24th October 2015.

• ## More puzzles to try-

• ### What is the logic behind these ?

3 + 3 = 3 5 + 4 = 4 1 + 0 = 3 2 + 3 = 4 ...Read More »
• ### Defective stack of coins puzzle

There are 10 stacks of 10 coins each. Each coin weights 10 gms. However, one stack of coins is defective ...Read More »
• ### Which clock works best?

Which clock works best? The one that loses a minute a day or the one that doesn’t work at all?Read More »
• ### (Advanced) Cheryl’s Birthday Puzzle

Paul, Sam and Dean are assigned the task of figuring out two numbers. They get the following information: Both numbers ...Read More »
• ### Five greedy pirates and gold coin distribution Puzzle

Five  puzzleFry ship’s pirates have obtained 100 gold coins and have to divide up the loot. The pirates are all ...Read More »
• ### Magical flowers!!

A  devotee goes to three temples,  temple1, temple2  and temple3  one after the other. In front of each temple, there ...Read More »
• ### Tuesday, Thursday what are other two days staring with T?

Four days are there which start with the letter ‘T‘. I can remember only two of them as “Tuesday , Thursday”. ...Read More »
• ### How could only 3 apples left

Two fathers took their sons to a fruit stall. Each man and son bought an apple, But when they returned ...Read More »
• ### How Many Eggs ?

A farmer is taking her eggs to the market in a cart, but she hits a  pothole, which knocks over ...Read More »
• ### HARD MATHS – How much faster is one train from other Puzzle

Two trains starting at same time, one from Bangalore to Mysore and other in opposite direction arrive at their destination ...Read More »
• ### Most Analytical GOOGLE INTERVIEW Question Revealed

Let it be simple and as direct as possible. Interviewer : Tell me how much time (in days) and money would ...Read More »
• ### Lateral thinking sequence Puzzle

Solve this logic sequence puzzle by the correct digit- 8080 = 6 1357 = 0 2022 = 1 1999 = ...Read More »
• ### How did he know?

A man leaves his house in the morning to go to office and kisses his wife. In the evening on ...Read More »
• ### Pizza Cost Math Brain Teaser

Jasmine, Thibault, and Noah were having a night out and decided to order a pizza for \$10. It turned out ...Read More »
• ### Which letter replaces the question mark

Which letter replaces the question markRead More »
• ### Which room is safest puzzle

A murderer is condemned to death. He has to choose between three rooms. The first is full of raging fires, ...Read More »
• ### Richie’s Number System

Richie established a very strange number system. According to her claim for different combination of 0 and 2 you will ...Read More »
• ### Srabon wanted to pass

The result of math class test came out. Fariha’s mark was an even number. Srabon got a prime!! Nabila got ...Read More »
• ### Become Normal!!

Robi is a very serious student. On the first day of this year his seriousness for study was 1 hour. ...Read More »
• ### Sakib Knows The Number!

Ragib: I got digits of a 2 digit number Sakib: Is it an odd? Ragib: Yes. Moreover, the sum of ...Read More »