CT085

Longest Collatz Sequence

MediumAcceptance: 0.0%

The Collatz sequence is defined as follows for a positive integer n: - If n is even, divide it by 2. - If n is odd, multiply by 3 and add 1. The sequence terminates when it reaches 1. For example, starting with 13: 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 (10 steps). Given N, find the starting number below N that produces the longest Collatz chain.

Example 1:

Input: 10
Output: 9

Example 2:

Input: 1000000
Output: 837799

Example 3:

Input: 100
Output: 97

Constraints:

1 <= N <= 10^6

Tags:

math collatz memoization dynamic-programming
Loading...
Test Cases:No test cases
No test cases available.