Weird Algorithm || Introductory Problem || CSES

#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007 ;
#define int long long int


int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif

    //----Solution-----//
    int n;
    cin>>n;
    while(n!=1){
        cout<<n<<" ";
        if(n&1){ // if n is not divisible by 3 then
            n=n*3+1; // multiply it by 3 and add 1
        }else{
            n/=2; // else divide it by 2
        }
    }
    cout<<n;


    return 0;
}

Comments