![]() |
VOOZH | about |
Juggler Sequence is a series of integers in which the first term starts with a positive integer number n and the remaining terms are generated from the immediate previous term using the below recurrence relation: .
Note:
Given a number n, find the Juggler Sequence for this number as the first term of the sequence until it becomes 1.
Examples:
Input: n = 9
Output: 9 27 140 11 36 6 2 1
Explanation: We start with 9 and use the above formula to get next terms.
Input: n = 6
Output: 6 2 1
Explanation: [61/2] = 2, [21/2] = 1.
Table of Content
The idea is to generate the Juggler sequence starting from n using recursion. At each step, print n, and if n == 1, stop. We compute the next term based on parity: if n is even, take √n, else take n × √n, and recursively repeat until 1 is reached.
9 27 140 11 36 6 2 1
Time Complexity: O(k), Where k is the number of terms in the Juggler sequence.
Auxiliary Space : O(k)
The idea is to simulate the Juggler sequence by iteratively applying the given rules. Starting from n, if it is odd, compute n * √n, otherwise compute √n, and continue until n becomes 1 while storing each value in a vector.
Let us understand with an example:
Input: n = 9
Start: res = [9]
Stop at n = 1, Output: 9 27 140 11 36 6 2 1
Juggler Sequence : 9 27 140 11 36 6 2 1
Time Complexity: O(k), Where k is the number of terms in the Juggler sequence.
Auxiliary Space : O(1)