I would like to understand how to calculate coordinates of boundaries group of objects as a parallelepiped in 3D game?

Let’s say for simplicity we have spheres randomly slowly moving in 3d space some of them cross each other from time to time and forming groups from 2 or more object locating very close to each other. Now I need to create dynamic parallelepiped surrounding this formed group as long as this group still formed because spheres moving constantly and such groups may appear and disappear chaotically.

So how to calculate coordinates of this parallelepiped faster? The naive approach I was thinking is just calculate distance of each object from each other individually to know to which group each sphere belongs which is slow O(n^2) time complexity. Is there more efficient solution to solve this problem?

Sorry if already similar question answered. I couldn’t find answer to my question so far.