715. Print All Odd Numbers up to N

EasyRecursionRecursion

Given a non-negative integer n, return all odd integers from 1 up to and including n (if n is odd) as a single string of numbers separated by single spaces, in increasing order. If there are no odd numbers in the range (i.e., n = 0), return an empty string. You must build the result recursively without loops.

Input: A single non-negative integer n.

Output: A space-separated string of odd numbers, or empty string if none.

Examples

Example 1
Input: 9
Output: 1 3 5 7 9
Explanation: Odd numbers up to 9.
Example 2
Input: 10
Output: 1 3 5 7 9
Explanation: 10 is even, so it is excluded.
Example 3
Input: 0
Output: 
Explanation: No odd numbers <= 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →