Maximum Collatz sequence length
Submissions: 1151   Accuracy:

27.11%

  Difficulty: Easy   Marks: 2

Starting with any positive integer N, we define the Collatz sequence corresponding to N as the numbers formed by the following operations:

NN/2 ( if N is even)
N → 3N + 1 (if N is odd)

i.e. If N is even, divide it by 2 to get N / 2. If N is odd, multiply it by 3 and add 1 to obtain 3N + 1.

It is conjectured but not yet proven that no matter which positive integer we start with; we always end up with 1.

For example, 10 → 5  → 16  → 8  → 4  → 2  → 1

Note: The sequence should end at the 1st occurence of integer 1.

The length of the Collatz sequence for some given N is defined as the number of numbers in the sequence starting with N and ending at 1.

Given a positive integer N, the task is to print the maximum Collatz sequence length among all numbers from 1 to N (both included).

Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of a single line containing a positive integer N.

Output:
Corresponding to each test case, in a new line, print the maximum Collatz sequence length among all numbers from 1 to N (both included).

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000000

Example:
Input
2
3
20
Output
8
21

Explanation:
For the 1st test case where N = 3
For N= 3 we need to check sequence length when sequence starts with 1,2, and 3.
when sequence starts with 1, it's : 1 length = 1
when sequence starts with 2, it's : 2->1, length = 2
when sequence starts with 3, it's : 3->10->5->16->8->4->2->1, length = 8, which is max of all.

** For More Input/Output Examples Use 'Expected Output' option **

Author: Hemang Sarkar


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.