From be4598fc53a9c4c9a29df09ad51c7eb6fa2f83da Mon Sep 17 00:00:00 2001 From: Ishita Mukherjee Date: Wed, 24 Jul 2024 19:04:28 +0530 Subject: [PATCH 1/2] Solution of Print Pattern is added --- .../Easy problems/Print-Pattern.md | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md diff --git a/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md b/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md new file mode 100644 index 000000000..5bc8419b1 --- /dev/null +++ b/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md @@ -0,0 +1,119 @@ +--- +id: nth-fibonacci-number +title: Nth Fibonacci Number +sidebar_label: Nth Fibonacci Number +tags: + - Recursion + - Algorithms +description: "This tutorial covers the solution to the Nth Fibonacci Number problem from the GeeksforGeeks." +--- +## Problem Description +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. + +## Examples + +**Example 1:** + +``` +Input: n = 16 +Output: 16 11 6 1 -4 1 6 11 16 +Explanation: The value decreases until it is greater than 0. After that it increases and stops when it becomes 16 again. +``` + +**Example 2:** + +``` +Input: n = 10 +Output: 10 5 0 5 10 +Explanation: It follows the same logic as per the above example. +``` + + + +Expected Time Complexity: $O(n)$ + +Expected Auxiliary Space: $O(n)$ for dynamic programming + +## Constraints + +* `-10^5 ≤ n ≤ 10^5` + +## Problem Explanation +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. + +## Code Implementation + + + + + + ```py + def pattern(n, initial=None, result=None): + if result is None: + result = [] + if initial is None: + initial = n + if n > 0: + result.append(n) + return pattern(n - 5, initial, result) + elif n < initial: + result.append(n) + return pattern(n + 5, initial, result) + return result + + ``` + + + + + + ```cpp + vector pattern(int n) { + vector result; + while (n > 0) { + result.push_back(n); + n -= 5; + } + while (n < result[0]) { + result.push_back(n); + n += 5; + } + return result; +} + + + ``` + + + + + + + + ```java +public List pattern(int n) { + List result = new ArrayList<>(); + while (n > 0) { + result.add(n); + n -= 5; + } + while (n < result.get(0)) { + result.add(n); + n += 5; + } + return result; +} + + ``` + + + + + +## Time Complexity + +* 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. + +## Space Complexity + +* The space complexity is O(n) because we are storing the sequence of numbers in the result list. From 1d71775e6e4d51b6f7f536ef7ff6c035379e3c16 Mon Sep 17 00:00:00 2001 From: Ishita Mukherjee Date: Wed, 24 Jul 2024 19:05:56 +0530 Subject: [PATCH 2/2] Updated --- .../gfg-solutions/Easy problems/Print-Pattern.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md b/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md index 5bc8419b1..0f42e4752 100644 --- a/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md +++ b/dsa-solutions/gfg-solutions/Easy problems/Print-Pattern.md @@ -1,14 +1,14 @@ --- -id: nth-fibonacci-number -title: Nth Fibonacci Number -sidebar_label: Nth Fibonacci Number +id: print-pattern +title: Print Pattern +sidebar_label: Print-Pattern tags: - Recursion - Algorithms -description: "This tutorial covers the solution to the Nth Fibonacci Number problem from the GeeksforGeeks." +description: "This tutorial covers the solution to the Print Pattern problem from the GeeksforGeeks." --- ## Problem Description -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. +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. ## Examples