From c96b02e390b0613bd179485a3c906b875c18fa46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan-Iulian=20Alecu?= <165364995+pascalecu@users.noreply.github.com> Date: Wed, 13 May 2026 22:38:20 +0300 Subject: [PATCH] Add Maximum Array Rotation in Ruby --- archive/r/ruby/maximum-array-rotation.rb | 37 ++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 archive/r/ruby/maximum-array-rotation.rb diff --git a/archive/r/ruby/maximum-array-rotation.rb b/archive/r/ruby/maximum-array-rotation.rb new file mode 100644 index 000000000..229e1e91d --- /dev/null +++ b/archive/r/ruby/maximum-array-rotation.rb @@ -0,0 +1,37 @@ +# 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)