Algorithm · Airbnb · Easy
The Collatz conjecture describes a sequence that starts from any positive integer. Starting with n, repeat the following rules until n equals 1: If n is even, set n to n / 2. If n is odd, set n to 3 * n + 1. The conjecture claims that this sequence always reaches 1 for every positive starting value. Your task is to return how many steps are needed to reach 1 from the given n. Constraints: $$1 \le n \le 10^6$$ Example 1: Input: n = 6 Output: 8 Explanation: The values in the…
Checking your access…