Maximum Subarray in Tcl

Published on 09 October 2025 (Updated: 09 October 2025)

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

Current Solution

proc usage {} {
	puts stderr {Usage: Please provide a list of integers in the format: "1, 2, 3, 4, 5"}
	exit 1
}

proc parseList {s} {
	set tokens [split [string trim $s] ","]
	if {[llength $tokens] < 1} { usage }

	set result {}

	set result {}
	foreach token $tokens {
		set t [string trim $token]
		if {$t eq "" || [catch {expr {int($t)}} val]} usage
		lappend result $val
	}
	return $result
}

proc maximumSubarraySum {numbers} {
    if {[llength $numbers] == 0} { return 0 }

    set currentSum [lindex $numbers 0]
    set maxSum $currentSum

    for {set i 1} {$i < [llength $numbers]} {incr i} {
        set val [lindex $numbers $i]
        set currentSum [expr {max($val, $currentSum + $val)}]
        set maxSum [expr {max($maxSum, $currentSum)}]
    }

    return $maxSum
}

if {$argc != 1} { usage }

set numbers [parseList [lindex $argv 0]]
puts [maximumSubarraySum $numbers]

Maximum Subarray in Tcl 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.