![]() |
VOOZH | about |
Given a number n, print the following pattern without using any loop.
n, n-5, n-10, ..., 0, 5, 10, ..., n+5, n
Examples :
Input: n = 16
Output: 16, 11, 6, 1, -4, 1, 6, 11, 16Input: n = 10
Output: 10, 5, 0, 5, 10
Follow the given steps to solve the problem:
Below is the implementation of the above approach:
16 11 6 1 -4 1 6 11 16
Time Complexity: O(N)
Auxiliary Space: O(N), stack space for the recursion
To solve the problem follow the below idea:
The above program works fine and prints the desired out but uses extra variables. We can use two print statements. The first one before the recursive call prints all decreasing sequences. The second one after the recursive call to print the increasing sequence
Below is the implementation of the above approach:
16 11 6 1 -4 1 6 11 16
Time Complexity: O(N)
Auxiliary Space: O(N), stack space for the recursion
Thanks to AKSHAY RATHORE for suggesting the above solution.