5 kyu

Maximum subarray sum

1,108 of 85,053knotman90

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

More By Author:

Check out these other kata created by knotman90

Stats:

CreatedOct 30, 2014
PublishedOct 30, 2014
Warriors Trained196961
Total Skips42411
Total Code Submissions422493
Total Times Completed85053
Haskell Completions1108
JavaScript Completions26926
Python Completions31970
Clojure Completions282
Java Completions10380
Ruby Completions1884
Go Completions1632
C# Completions4581
C Completions1976
C++ Completions4726
Kotlin Completions702
Rust Completions693
Scala Completions92
COBOL Completions7
Total Stars4304
% of votes with a positive feedback rating89% of 6719
Total "Very Satisfied" Votes5419
Total "Somewhat Satisfied" Votes1067
Total "Not Satisfied" Votes233
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