Maximum Subarray in Swift

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

Welcome to the Maximum Subarray 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 in the format: "1, 2, 3, 4, 5"
    """

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

extension Collection {
    subscript(safe index: Index) -> Element? {
        indices.contains(index) ? self[index] : nil
    }
}

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 maxSubarraySum(_ numbers: [Int]) -> Int {
    var best = numbers[0]
    var current = numbers[0]

    for value in numbers.dropFirst() {
        current = max(value, current + value)
        best = max(best, current)
    }

    return best
}

let args = CommandLine.arguments

guard let list = parseList(args[safe: 1]) else {
    print(usage)
    exit(1)
}

let result = maxSubarraySum(list)
print(result)

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