719. Print Fibonacci Series up to N Terms

EasyRecursionRecursion

Given a non-negative integer n, return the first n terms of the Fibonacci sequence as a single string of numbers separated by single spaces. The sequence starts with 0 and 1 (i.e., F(0) = 0, F(1) = 1, F(k) = F(k-1) + F(k-2)). If n = 0, return an empty string.

Input: A single non-negative integer n.

Output: A space-separated string of the first n Fibonacci numbers, or an empty string if n = 0.

Examples

Example 1
Input: 5
Output: 0 1 1 2 3
Explanation: First 5 Fibonacci numbers.
Example 2
Input: 1
Output: 0
Explanation: Only F(0) = 0.
Example 3
Input: 0
Output: 
Explanation: No terms requested -> empty string.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →