5 kyu

Maximum subarray sum

1,098 of 83,699knotman90

Description:

The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:

maxSequence [-2, 1, -3, 4, -1, 2, 1, -5, 4]
-- should be 6: [4, -1, 2, 1]
maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
// should be 6: [4, -1, 2, 1]
max_sequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
# should be 6: [4, -1, 2, 1]
(max-sequence [-2, 1, -3, 4, -1, 2, 1, -5, 4])
;; should be 6: [4, -1, 2, 1]
Max.sequence(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});
// should be 6: {4, -1, 2, 1}
Max.sequence(Array(-2, 1, -3, 4, -1, 2, 1, -5, 4));
// should be 6: Array(4, -1, 2, 1)
maxSequence(listOf(-2, 1, -3, 4, -1, 2, 1, -5, 4));
// should be 6: listOf(4, -1, 2, 1)
maxSequence({-2, 1, -3, 4, -1, 2, 1, -5, 4}, 9)
// should return 6, from sub-array: {4, -1, 2, 1}
maxSequence({-2, 1, -3, 4, -1, 2, 1, -5, 4});
//should be 6: {4, -1, 2, 1}
max_sequence(&[-2, 1, -3, 4, -1, 2, 1, -5, 4]);
//should be 6: [4, -1, 2, 1]
       maxSequence [-2, 1, -3, 4, -1, 2, 1, -5, 4]
      * should be 6: [4, -1, 2, 1]

Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.

Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.

Algorithms
Lists
Dynamic Programming
Fundamentals
Performance

Similar Kata:

More By Author:

Check out these other kata created by knotman90

Stats:

CreatedOct 30, 2014
PublishedOct 30, 2014
Warriors Trained194470
Total Skips42096
Total Code Submissions418697
Total Times Completed83699
Haskell Completions1098
JavaScript Completions26562
Python Completions31409
Clojure Completions281
Java Completions10254
Ruby Completions1872
Go Completions1593
C# Completions4496
C Completions1935
C++ Completions4623
Kotlin Completions693
Rust Completions658
Scala Completions90
COBOL Completions6
Total Stars4269
% of votes with a positive feedback rating89% of 6681
Total "Very Satisfied" Votes5385
Total "Somewhat Satisfied" Votes1065
Total "Not Satisfied" Votes231
Ad
Contributors
  • knotman90 Avatar
  • jhoffner Avatar
  • xcthulhu Avatar
  • Azuaron Avatar
  • bkaes Avatar
  • itsPG Avatar
  • Unnamed Avatar
  • yantonov Avatar
  • GiacomoSorbi Avatar
  • MaLiN2223 Avatar
  • Chrono79 Avatar
  • acjoker Avatar
  • Voile Avatar
  • ice1000 Avatar
  • rowcased Avatar
  • KataSideKick Avatar
  • faktotum85 Avatar
  • hobovsky Avatar
  • mouloud Avatar
  • testfirstcoder Avatar
  • username0 Avatar
  • kirull Avatar
  • akar-0 Avatar
  • Just4FunCoder Avatar
  • benjaminzwhite Avatar
  • oliver-m Avatar
  • KayleighWasTaken Avatar
Ad