How to center align last row of parts?

I am trying to center align the last row of parts so it looks like this:

The red part is the origin and the gray parts are positioned in a grid.

Here is what I got so far:

My code:

local MAX = 3
local GAP = 4

local function update()
	local origin = workspace.origin:GetPivot()
	local parts = workspace.Folder:GetChildren()
	
	local r = #parts % MAX
	for i, part in ipairs(parts) do
		local row = math.floor((i-1)/MAX)
		local col = (i + (MAX-1)) % MAX
		local offset = CFrame.new(
			((MAX*GAP)+(col*GAP)) * 0.5,
			0,
			GAP * (row + 1))
		
		part:PivotTo(origin * offset)
		part.Name = tostring(i)
		part.Parent = workspace.Folder
	end
end

update()

workspace.Folder.ChildAdded:Connect(update)
workspace.Folder.ChildRemoved:Connect(update)

I am just trying to figure the correct “formula” for the X value of the offset CFrame.