705. Reverse an Array Using Recursion

EasyRecursionRecursion

Given an integer array nums, return a new array containing the elements of nums in reverse order. You must implement the reversal recursively without using any loops.

Input: An integer array nums.

Output: Return the reversed array as a comma-separated list inside square brackets, e.g. [3,2,1].

Examples

Example 1
Input: [1,2,3,4,5]
Output: [5,4,3,2,1]
Explanation: Swap pairs from outside in: (1,5), (2,4); 3 stays.
Example 2
Input: [7]
Output: [7]
Explanation: Single element is its own reverse.
Example 3
Input: [1,2]
Output: [2,1]
Explanation: Two elements swap.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →