|
| 1 | +--- |
| 2 | +id: print-pattern |
| 3 | +title: Print Pattern |
| 4 | +sidebar_label: Print-Pattern |
| 5 | +tags: |
| 6 | + - Recursion |
| 7 | + - Algorithms |
| 8 | +description: "This tutorial covers the solution to the Print Pattern problem from the GeeksforGeeks." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | +Print a sequence of numbers starting with nn, without using a loop. Replace `nn` with `n−5n - 5n−5` until `n≤0n` `\leq 0n≤0`. Then, replace n with `n+5n + 5n+5` until `nn` regains its initial value. Complete the function pattern(n) which takes n as input and returns a list containing the pattern. |
| 12 | + |
| 13 | +## Examples |
| 14 | + |
| 15 | +**Example 1:** |
| 16 | + |
| 17 | +``` |
| 18 | +Input: n = 16 |
| 19 | +Output: 16 11 6 1 -4 1 6 11 16 |
| 20 | +Explanation: The value decreases until it is greater than 0. After that it increases and stops when it becomes 16 again. |
| 21 | +``` |
| 22 | + |
| 23 | +**Example 2:** |
| 24 | + |
| 25 | +``` |
| 26 | +Input: n = 10 |
| 27 | +Output: 10 5 0 5 10 |
| 28 | +Explanation: It follows the same logic as per the above example. |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +Expected Time Complexity: $O(n)$ |
| 34 | + |
| 35 | +Expected Auxiliary Space: $O(n)$ for dynamic programming |
| 36 | + |
| 37 | +## Constraints |
| 38 | + |
| 39 | +* `-10^5 ≤ n ≤ 10^5` |
| 40 | + |
| 41 | +## Problem Explanation |
| 42 | +Print a sequence of numbers starting with nn, without using a loop. Replace `nn` with `n−5n - 5n−5` until `n≤0n` `\leq 0n≤0`. Then, replace n with `n+5n + 5n+5` until nn regains its initial value. Complete the function pattern(n) which takes n as input and returns a list containing the pattern. |
| 43 | + |
| 44 | +## Code Implementation |
| 45 | + |
| 46 | +<Tabs> |
| 47 | + <TabItem value="Python" label="Python" default> |
| 48 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 49 | + |
| 50 | + ```py |
| 51 | + def pattern(n, initial=None, result=None): |
| 52 | + if result is None: |
| 53 | + result = [] |
| 54 | + if initial is None: |
| 55 | + initial = n |
| 56 | + if n > 0: |
| 57 | + result.append(n) |
| 58 | + return pattern(n - 5, initial, result) |
| 59 | + elif n < initial: |
| 60 | + result.append(n) |
| 61 | + return pattern(n + 5, initial, result) |
| 62 | + return result |
| 63 | + |
| 64 | + ``` |
| 65 | + |
| 66 | + </TabItem> |
| 67 | + <TabItem value="C++" label="C++"> |
| 68 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 69 | + |
| 70 | + ```cpp |
| 71 | + vector<int> pattern(int n) { |
| 72 | + vector<int> result; |
| 73 | + while (n > 0) { |
| 74 | + result.push_back(n); |
| 75 | + n -= 5; |
| 76 | + } |
| 77 | + while (n < result[0]) { |
| 78 | + result.push_back(n); |
| 79 | + n += 5; |
| 80 | + } |
| 81 | + return result; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | + ``` |
| 86 | +
|
| 87 | + </TabItem> |
| 88 | +
|
| 89 | +
|
| 90 | + <TabItem value="Java" label="Java" default> |
| 91 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 92 | +
|
| 93 | + ```java |
| 94 | +public List<Integer> pattern(int n) { |
| 95 | + List<Integer> result = new ArrayList<>(); |
| 96 | + while (n > 0) { |
| 97 | + result.add(n); |
| 98 | + n -= 5; |
| 99 | + } |
| 100 | + while (n < result.get(0)) { |
| 101 | + result.add(n); |
| 102 | + n += 5; |
| 103 | + } |
| 104 | + return result; |
| 105 | +} |
| 106 | +
|
| 107 | + ``` |
| 108 | + |
| 109 | + </TabItem> |
| 110 | +</Tabs> |
| 111 | + |
| 112 | + |
| 113 | +## Time Complexity |
| 114 | + |
| 115 | +* The iterative approach has a time complexity of $O(n)$ because we are iterating through the sequence of numbers twice: once from n to 0, and once from 0 to n. |
| 116 | + |
| 117 | +## Space Complexity |
| 118 | + |
| 119 | +* The space complexity is O(n) because we are storing the sequence of numbers in the result list. |
0 commit comments