Maximum Array Rotation in Ruby

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

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

Current Solution

# frozen_string_literal: true

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

def usage!
  warn USAGE
  exit 1
end

def parse_list(str)
  str.split(",").map { Integer(it.strip) }
rescue ArgumentError, NoMethodError
  usage!
end

def max_rotation_sum(arr)
  n = arr.size
  total_sum = arr.sum
  current = arr.each_with_index.sum { |v, i| i * v }

  max = current

  (1...n).each do |i|
    last = arr[n - i]
    current += total_sum - n * last
    max = current if current > max
  end

  max
end

raw = ARGV.first
usage! if raw.nil? || raw.strip.empty?

arr = parse_list(raw)

puts max_rotation_sum(arr)

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