Print Series

You have given two positive integers N and K. Your task is to print a series of numbers i.e subtract K from N until it becomes 0 or negative then add K until it becomes N. You need to do this task without using any loop.

For Example:
For  N = 5 , K = 2 
Series = [ 5, 3, 1, -1, 1, 3, 5]
Input format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run 

The first line of each test contains two space-separated integers N and K. 
Output format :
For each test case, print a single line containing the required series.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100   
1 <= N <= 3000 
1 <= K <= N 

Time Limit: 1sec
CodingNinjas
author
2y
Recursion

The idea here is to use recursion,we can print current N two times in the current recursion stack. One for decreasing order and one for increasing order.

Steps :

  • Declare an empty array to s...read more
Help your peers!
Add answer anonymously...
MAQ Software Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter