← Recursion

Recursive Array Sum

hard

Sum the first n elements of arr recursively – the same problem as this module’s very first “Array Sum” exercise, this time without a loop.

The recursive step reuses two ideas from earlier in this module at once: arr[0] peels off the first element (an ordinary array index), and arr + 1 – pointer arithmetic – produces a pointer to “the array starting one element later,” which paired with n - 1 describes exactly the remaining elements. array_sum(arr + 1, n - 1) is a completely ordinary recursive call on a smaller version of the same problem; it just happens that “smaller” here means “starting one element further into the same block of memory,” not a smaller number passed by value.

Your solution

"Run" uses the sample stdin ("4\n1 2 3 4"). "Submit" checks your code against all 4 test cases.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution