आसान 0 हल किए गए 0% स्वीकृति

Collatz Sequence

Follow the halve-or-triple-and-add-one rule until reaching 1.

Start with n. If it is even, divide it by 2; if it is odd, multiply it by 3 and add 1. Repeat until the value is 1. Include both the starting value and the final 1 in your answer.

Implement collatz_sequence(n).

Input

n: The positive starting integer.

Output

Return the complete sequence in order.

सीमाएँ

1 ≤ n ≤ 10,000. Inputs in this range reach 1 under this rule; all sequence values fit a signed 64-bit integer.

उदाहरण

उदाहरण 1
इनपुट:
n = 6

अपेक्षित आउटपुट:
expected = [6, 3, 10, 5, 16, 8, 4, 2, 1]
इनपुट:
long long n = 6;

अपेक्षित आउटपुट:
std::vector<long long> expected = {6, 3, 10, 5, 16, 8, 4, 2, 1};
इनपुट:
long n = 6;

अपेक्षित आउटपुट:
long[] expected = new long[] {6, 3, 10, 5, 16, 8, 4, 2, 1};

व्याख्या

6 is even, so it becomes 3. Then 3 becomes 10. Continue applying the rule until 1.

उदाहरण 2
इनपुट:
n = 1

अपेक्षित आउटपुट:
expected = [1]
इनपुट:
long long n = 1;

अपेक्षित आउटपुट:
std::vector<long long> expected = {1};
इनपुट:
long n = 1;

अपेक्षित आउटपुट:
long[] expected = new long[] {1};

व्याख्या

The starting value is already 1, so no transformations are needed.

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।