You do not need to stay in the octave of the key you are in, or any other octave for that matter. The formula of every other note, or (1, 3 ,5) for Maj, (1, b3, 5) for min etc help you pick the correct notes but those notes can (1) be played in any order creating inversions of the chords or triads, (2) include repeated notes to make the chord more full sounding, and (3) be anywhere in the range of the instrument in theory.
Take for example the Key of C. Think of your available notes as being the repeating sequence,
C D E F G A B C D E F G A B C D E F G A B C D E F G A B C ...
We sometimes label the letters with a number to remind us of what octave we are in, for example,
C1 D1 E1 F1 G1 A1 B1 C2 D2 E2 F2 G2 A2 B2 C3 D3 E3 F3 G3 A3 B3 C4 D4 E4 F4 G4 A4 B4 C5 ...
Then you can grab any of the triads you listed in their "root" position, (1, 3, 5). For example your G starting on G1 is (G1, B1, D2) etc.
You can get all your chords to stay within an octave by rearranging the notes to make an inversion. These are much more interesting in terms of chord movement and harmony.
Root position (I had learned or remember inversion) has the root note, the 1 in the bass. The first inversion has the second note, the 3rd in the bass, and the second inversion has the 5th in the bass.
In the Key of C all your triads can fit into one octave and will have the following format.
I = C Maj (C, E, G) root position
ii = D min (D, F, A) root position
iii = E min (E, G, B) root position
IV = F Maj (F, A, C) root position
V = G Maj (D, G, B) first inversion
vi = A min (E, A, C) first inversion
vii = B dim (F, D, B) second inversion
Things get more interesting with 7th chords but the same logic applies.