Maximum Array Rotation in Powershell

Published on 16 July 2025 (Updated: 16 July 2025)

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

Current Solution

function Show-Usage() {
    Write-Host 'Usage: please provide a list of integers (e.g. "8, 3, 1, 2")'
    Exit 1
}

function Parse-IntList([string]$Str) {
    @($Str.Split(",") | ForEach-Object { [int]::Parse($_) })
}

function Invoke-MaximumArrayRotation([array]$Values) {
    # Calculate sum and initial array rotation
    $s = ($Values | Measure-Object -Sum).Sum
    $w = 0
    for ($i = 0; $i -lt $Values.Length; $i++) {
        $w += $i * $Values[$i]
    }

    # Initialize maximum array rotation
    $wMax = $w

    # Adjust array rotation and update maximum
    $n = $Values.Length
    for ($i = 0; $i -lt ($n - 1); $i++) {
        $w += $n * $Values[$i] - $s
        $wMax = [int][Math]::Max($wMax, $w)
    }

    $wMax
}

if ($args.Length -lt 1) {
    Show-Usage
}

try {
    $values = Parse-IntList $args[0]
} catch {
    Show-Usage
}

$maxValue = Invoke-MaximumArrayRotation $values
Write-Host $maxValue

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