An unclear thing about one challenge

The penny finally dropped for me with this comment ’ sum([1, 2, 3], 2) , so we want to get a sum of first two elements.’ As much as I read and re-read the question, i didn’t understand it. It wasn’t clear to me that n represented exactly how many of the first elements of the array were to be added (multiplied in the example), so I was stuck for days. Should I have recognised this in the format (arr, n) from a previous exercise that I’ve forgotten? This question is for the benefit of future strugglers!


I’m trying to make sense of that one myself. I get 7 as a result.

edit: I think I was stopping once n hits 0, but didn’t return what is AT 0, which is 2. 2 + 7 = 9

Hi, do you have a moment to explain why
sum([1] , 0) results in 0?
I don’t know how the other solutions apply here. Maybe this is a stupid question, but is it b/c n=0 already, so it returns 0 as the base case asks?
Thanks in advance.

yeah, it says so there


Yes, but since 1 is at index position 0 I thought I was missing something.

the n is the number of array elements to sum, it is not an index


Ok, well now I’m even more confused, b/c I thought the n referred to the index position in arr[n-1]

Write a recursive function, sum(arr, n) , that returns the sum of the first n elements of an array arr .

the n is the number of elements to sum, but the arrays are indexed starting from 0, so to sum n elements you sum from index 0 to index n-1


I had the right answer, but was still not getting how it worked. vidmate insta save

I don’t understand what you are asking.

Ah, okay, that helps. Thank you!

Just thinking out loud with some pseudocode here:
So with, let’s say,
sum([10,11,12], 3) + arr[n-1]
sum([10,11,12,13], 2) + 12 (which is the index of n of 2, or arr[n-1]).
At this point you’re only getting back 12, right? You’re not adding that 12 to the 2(n) in the function, correct? It’s not 2 + 12 + whatever comes next, right?
You’re just waiting for that 2 to get to 0 for the recursion to end and return everything, correct?
So next would be:
sum([10,11,12], 1) + 11 (+ 12) // now we have 23
sum([10,11,12], 0) + 10 (+ 11 + 12) // and now we stop and return 33
Is that right?

this two are not the same, you have changed a 2 to a 3

inside the function call sum([10,11,12], 3) you have this:

no, there is no getting back for now, the line
return sum([10, 11, 12], 2) + 12 stays unsolved until the function call inside it has an output

1 Like

Doh! I left something out in my own example!
Ok, sorry, let me try again.
If we start with
sum([10,11,12,13], 3) we want the sum of those three elements, right?
sum([10,11,12,13], 2) + arr[12]
I want to stop right there, just to clarify. While we’re not returning that just yet, the 12 alone is going in to the call stack, correct? We’re not adding the 12 to the 2 in the function call, correct?

now your 3 has become a 12

the function as a whole pause and wait in the call stack until the function inside it is resolved, the 12 alone does not go in the callstack

1 Like

That’s what we want right? If n=3, in my example, and n-1 = 2 that is 12 in the array.

What I really wanted to know was that we are adding 12 to 11 to 10, not 12 to 2 and so on. N in the recursive function is only serving the purpose of counting down, right?

Thanks for the help, btw. I’m sure this is very basic and nothing to you, but just trying to wrap my head around it.

you do not want arr[12], your array does not have an element at index 12

n is only function parameter and used to access the array at index n-1, it is not involved in a sum anywhere

1 Like

but 12 is at index 2, correct? That’s what I meant.

arr[2] and arr[12] are not the same thing

Right, ok. Then how would you say what arr[2] equals?