Maximum Array Rotation in Ada

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

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

Current Solution

with Ada.Text_IO;       use Ada.Text_IO;
with Ada.Command_Line;  use Ada.Command_Line;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Maps;  use Ada.Strings.Maps;
with Ada.Containers.Vectors;

procedure Maximum_Array_Rotation is

   Data_Format_Error : exception;

   function Clean (S : String) return String
   is (Trim (S, Ada.Strings.Both));

   function To_Int (S : String) return Integer is
      Cleaned : constant String := Clean (S);
   begin
      return Integer'Value (Cleaned);
   exception
      when Constraint_Error =>
         raise Data_Format_Error with "Invalid integer: '" & Cleaned & "'";
   end To_Int;

   package Integer_Vectors is new
     Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Integer);

   subtype Int_List is Integer_Vectors.Vector;

   function To_Int_List (Raw_String : String) return Int_List is
      Result : Int_List;
      Start  : Positive := Raw_String'First;
      Finish : Natural;
      Seps   : constant Character_Set := To_Set (", ");
   begin
      while Start <= Raw_String'Last loop
         Find_Token
           (Raw_String, Seps, Start, Ada.Strings.Outside, Start, Finish);

         exit when Start > Finish;

         Result.Append (To_Int (Raw_String (Start .. Finish)));

         Start := Finish + 1;
      end loop;

      if Result.Is_Empty then
         raise Data_Format_Error with "List is empty";
      end if;

      return Result;
   end To_Int_List;

   function Max_Rotation (A : Int_List) return Integer is
      N : constant Natural := Natural (A.Length);

      Total : Integer := 0;
      Curr  : Integer := 0;
      Best  : Integer;
   begin
      if N = 0 then
         return 0;
      end if;

      for I in A.First_Index .. A.Last_Index loop
         Total := Total + A.Element (I);
         Curr := Curr + (I - A.First_Index) * A.Element (I);
      end loop;

      Best := Curr;

      for K in 1 .. N - 1 loop
         Curr := Curr + Total - N * A.Element (A.Last_Index - K + 1);
         Best := Integer'Max (Best, Curr);
      end loop;

      return Best;
   end Max_Rotation;

   procedure Print_Usage is
   begin
      Put_Line
        ("Usage: please provide a list of integers (e.g. ""8, 3, 1, 2"")");
   end Print_Usage;

begin
   if Argument_Count /= 1 then
      Print_Usage;
      Set_Exit_Status (Failure);
      return;
   end if;

   begin
      declare
         Arr : constant Int_List := To_Int_List (Argument (1));
      begin
         Put_Line (Integer'Image (Max_Rotation (Arr)));
      end;
   exception
      when Data_Format_Error | Constraint_Error =>
         Print_Usage;
         Set_Exit_Status (Failure);
   end;

end Maximum_Array_Rotation;

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