Recursive Sum

Easy~10 min

Given an array of integers nums, return the sum of all elements using recursion.

You must solve this problem recursively — do not use any loops (for, while, etc.).

For example, if the input is [1, 2, 3, 4, 5], the output should be 15.

Examples

Example 1
Input: nums = [1, 2, 3, 4, 5]
Output: 15
Explanation: 1 + 2 + 3 + 4 + 5 = 15.
Example 2
Input: nums = []
Output: 0
Explanation: An empty array has a sum of 0.
Example 3
Input: nums = [-1, 1]
Output: 0
Explanation: -1 + 1 = 0.

Constraints

  • 0 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • You must use recursion (no loops).
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run