March 11, 2010

Largest Sum of Consecutive Numbers

Problem: Given an array of N integers (both positive and negative), find the sub-sequence with largest sum.

For ex: Let A = {1 2 -5 4 5 -1 2 -11} then largest sum is 10 (start = 4, end = 7)

Solution: show

Problem: Given an array of N integers (both positive and negative), find the sub-sequence with largest absolute sum. This problem differs from the one at the top as we want the absolute sum (taking mod)

For ex: Let A = {1 2 -5 4 5 -1 2 -11} then largest absolute sum is 11.

Solution: show

1 comment:

  1. Hi can you please point out how max will work for absolute sum. Say if we have sequence A = {1 2 -5 4 5 -1 -2 -11}. Then the absolute sum is -14. Instead using something similar to part a) whenever sum(not absolute) and current element are of opposite sign just ignore the previous sequence since two opposite signs will always decrease the magnitude.

    ReplyDelete