Maximum Array Rotation in Swift

Published on 03 May 2026 (Updated: 03 May 2026)

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

Current Solution

import Foundation

let usage = """
    Usage: please provide a list of integers (e.g. "8, 3, 1, 2")
    """

extension StringProtocol {
    var trimmed: String { trimmingCharacters(in: .whitespacesAndNewlines) }
}

func parseList(_ input: String?) -> [Int]? {
    guard let input = input, !input.isEmpty else { return nil }

    let values =
        input
        .split(separator: ",")
        .map { $0.trimmed }
        .compactMap(Int.init)

    return values.isEmpty ? nil : values
}

func maximumArrayRotation(_ nums: [Int]) -> Int {
    let n = nums.count
    let totalSum = nums.reduce(0, +)

    var current = nums.enumerated().reduce(0) { $0 + $1.element * $1.offset }
    var best = current

    var suffix = nums

    for _ in 1..<n {
        let last = suffix.removeLast()
        suffix.insert(last, at: 0)

        current += totalSum - n * last
        best = max(best, current)
    }

    return best
}

let args = CommandLine.arguments

guard
    args.count == 2,
    let input = args.dropFirst().first,
    let numbers = parseList(input)
else {
    print(usage)
    exit(1)
}

print(maximumArrayRotation(numbers))

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