I am working through a Udemy algorithms and data structures course after completing the course on Codecademy. I have learnt big O notation but am still not that confident with the subject,

I just solved the following code challenge:

Write a function called maxSubArray which accepts an array of integers and a number called n. The function should calculate the maximum sum of n consecutive elements in the array. For example:

`maxSubArray([1,2,5,2,8,1,5], 2) // Return 10`

`maxSubArray([1,2,5,2,8,1,5], 4) // Return 17`

`maxSubArray([1,2,3,4,5,6], 1) // Return 6`

This is my solution:

```
function maxSubArray(arr, n) {
let maxSum = arr[0];
let temp;
// Check n is not greater than array length.
if (n > arr.length) return null;
// Loop over each element within array.
for (let i = 0; i < arr.length - n + 1; i++) {
temp = 0;
for (let j = 0; j < n; j++ ) {
// Temp is equal to temp + element.
temp = temp + arr[j + i];
}
// If temp is greater than maxSum, update maxSum to value in temp.
if (temp > maxSum) maxSum = temp;
}
return maxSum;
}
```

Although I have two nested loops which I know is normally a dead give away of a quadratic runtime, my second loop will only run n number of times? If my array is of length 5 and n is 2 surely my operations are O(10) â†’ O(n)? If it were quadratic and not linear, surely the number of operations would be 25, assuming length was still 5? The video I watched describes the above approach as having a quadratic runtime.

Thank you (