Maximum Subarray in Mathematica

Published on 28 April 2022 (Updated: 02 February 2023)

Maximum Subarray in Mathematica

Welcome to the Maximum Subarray in Mathematica page! Here, you’ll find the source code for this program as well as a description of how the program works.

Current Solution

(* Code *)

(* The actual maximum subarray operating on Mathematica lists, using essentially Kadane's algorithm: *)

maximumSubarray = a \[Function] Max[
    FoldList[
     Max[#2 (* A[i] *) , #2 + #1 (* A[i] + local-maximum *)] &,
     a]];

(* The outer function provides the 'user interface': *)

maximumSubarrayMain = l \[Function] Catch[
    Module[{e = "Usage: Please provide a list of at least two integers to sort in the format: \"1, 2, 3, 4, 5\"",
      fromNegativeDigits},
     (* oddly, Mathematica doesn't appear to provide a function which can parse strings representing negative integers *)
     fromNegativeDigits = Piecewise[{
         {FromDigits[#], StringMatchQ[#, DigitCharacter ..]},
         {-FromDigits[StringDrop[#, 1]], 
          StringMatchQ[#, "-" ~~ DigitCharacter ..]}},
        Throw[e]] &;
     maximumSubarray @ Map[
       (* convert string to integer, or throw *)
       fromNegativeDigits,
       (* construct arguments to maximum subarray *)
       StringSplit[If[StringLength[l] > 0, l, Throw[e]], ", "],
       {-1} (* at each leaf *)]]];


(* Valid Tests *)

Print /@ maximumSubarrayMain /@ {
    "1, 2, 3",
    "-1, -2, -3",
    "-2, -1, 3, 4, 5",
    "-1, -4, 2, 3, -3, -4, 9",
    "-1, -4, 2, 9, -3, -4, 9"
    };


(* Invalid Tests *)

maximumSubarrayMain[""]

Maximum Subarray in Mathematica was written by:

If you see anything you’d like to change or update, please consider contributing.

How to Implement the Solution

No ‘How to Implement the Solution’ section available. Please consider contributing.

How to Run the Solution

No ‘How to Run the Solution’ section available. Please consider contributing.