Print Array in Reverse

Given an integer array nums, print all its elements in reverse 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 reversed 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: 50 40 30 20 10

Explanation: Starting from the final element, the array is printed recursively toward the first element.

Example 2:

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

Output: -10 15 0 -5

Explanation: Every element is printed in the opposite order from its position in nums.

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 printArrayReverse(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums