Junwon Kim.

CSES - 1622 Creating String

CSES - 1622 Creating String

Problem Solving, CSES problem set

Problem & Approach

Given a positive integer n, repeatedly apply: divide by 2 if even, multiply by 3 and add 1 if odd. Print all values until n becomes 1.

Approach

  • Simulation
    • Simply simulate the process as described.
    • Note that intermediate values can exceed the initial range of n, so use long long.

Related Concepts

  1. Simulation

Implementation with C++

#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; while (n != 1) { cout << n << " "; if (n % 2 == 0) n /= 2; else n = 3 * n + 1; } cout << 1 << "\n"; return 0; }

Contact.

© 2025 Junwon Kim. All Rights Reserved.