5 kyu

Compute the Largest Sum of all Contiguous Subsequences

413 of 1,160LesRamer

Description:

Given an array of numbers, calculate the largest sum of all possible blocks of consecutive elements within the array. The numbers will be a mix of positive and negative values. If all numbers of the sequence are nonnegative, the answer will be the sum of the entire array. If all numbers in the array are negative, your algorithm should return zero. Similarly, an empty array should result in a zero return from your algorithm.

largestSum([-1,-2,-3]) == 0
largestSum([]) == 0
largestSum([1,2,3]) == 6

Easy, right? This becomes a lot more interesting with a mix of positive and negative numbers:

largestSum([31,-41,59,26,-53,58,97,-93,-23,84]) == 187

The largest sum comes from elements in positions 3 through 7: 59+26+(-53)+58+97 == 187

Once your algorithm works with these, the test-cases will try your submission with increasingly larger random problem sizes.

Mathematics
Fundamentals
Algorithms

Stats:

CreatedSep 21, 2015
PublishedSep 21, 2015
Warriors Trained2740
Total Skips170
Total Code Submissions4969
Total Times Completed1160
JavaScript Completions413
C# Completions154
Haskell Completions47
Ruby Completions45
Python Completions450
F# Completions29
Go Completions57
Rust Completions44
D Completions6
Java Completions27
Total Stars79
% of votes with a positive feedback rating94% of 203
Total "Very Satisfied" Votes181
Total "Somewhat Satisfied" Votes19
Total "Not Satisfied" Votes3
Total Rank Assessments10
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • LesRamer Avatar
  • kazk Avatar
  • Ze-Marcos Avatar
  • dcieslak Avatar
  • KenKamau Avatar
  • hobovsky Avatar
  • akar-0 Avatar
  • Mednoob Avatar
  • brodiemark Avatar
  • saudiGuy Avatar
Ad