Print Array - II

Given an integer array nums, print all its elements in the same order using recursion.

Print consecutive elements separated by a single space. Do not print a trailing space after the final element. Print a newline character after the complete array.

Note:

A normal iterative solution will pass the test cases, but the goal of this problem is to solve it using recursion and strengthen your understanding of recursive calls and base cases.

Example 1:

Input: nums = [10, 20, 30, 40, 50]

Output: 10 20 30 40 50

Explanation: The elements are printed recursively in the same order as they appear in nums.

Example 2:

Input: nums = [-5, 0, 15, -10]

Output: -5 0 15 -10

Explanation: Every element, including zero and negative values, is printed in its original position.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [3, 2, 6, -6, 2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 100
  • -105 <= nums[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
void printArray(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums