Maximum Array Rotation in Tcl

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

Welcome to the Maximum Array Rotation 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 (e.g. "8, 3, 1, 2")}
	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 maximumRotationSum {numbers} {
	set n [llength $numbers]
	if {$n == 0} { usage }

	set totalSum 0
	set currentSum 0
	for {set i 0} {$i < $n} {incr i} {
		set val [lindex $numbers $i]
		incr totalSum $val
		incr currentSum [expr {$val * $i}]
	}

	set maxSum $currentSum

	for {set i 1} {$i < $n} {incr i} {
		set rotatedVal [lindex $numbers [expr {$n - $i}]]
		set currentSum [expr {$currentSum + $totalSum - $n * $rotatedVal}]
		if {$currentSum > $maxSum} { set maxSum $currentSum }
	}

	return $maxSum
}

if {$argc != 1} { usage }

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

Maximum Array Rotation 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.