714. Print All Even Numbers up to N

EasyRecursionRecursion

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

Input: A single non-negative integer n.

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

Examples

Example 1
Input: 10
Output: 2 4 6 8 10
Explanation: All evens from 2 to 10.
Example 2
Input: 1
Output: 
Explanation: No even numbers in [2,1] -> empty string.
Example 3
Input: 7
Output: 2 4 6
Explanation: Evens up to 7 are 2, 4, 6.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →