Time limit exceeds in weird algorithm problem

here is the link for the problem CSES - Weird Algorithm
and this is my code

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
 
void solve(int n){
    while (n!=1){
        cout<<n<<" ";
        if(n%2==0){
            n=n/2;
        }else{
            n=n*3+1;
        }
    }
    cout<<n;
    
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin>>n;
    solve(n);
}

The solution to this problem is described in the USACO Guide; try looking up the problem using All Problems