Print Pattern microsoft interview question recursion from gfg solution -
Problem statement-
Print a sequence of numbers starting with N, without using loop, in which A[i+1] = A[i] - 5, if A[i]>0, else A[i+1]=A[i] + 5 repeat it until A[i]=N.
Input:
The first line contains an integer T, number of test cases. Then following T lines contains an integer N.
Output:
For each test case, print the pattern in newline with space separated integers.
Constraints:
0< N < 10000
Example:
Input:
2
16
10
Output:
16 11 6 1 -4 1 6 11 16
10 5 0 5 10
Explanation:
We basically first reduce 5 one by one until we reach a negative or 0. After we reach 0 or negative, we one by one add 5 until we reach N.
solution-
It has a simple recursive solution this problem can be solved by two recursive approaches. I have mentioned both of approach in my code check it below
This is my approach try to understand if not getting comment below I will reply you
Comments
Post a Comment