Intro DS & Algorithms Flashcards

1
Q

WOW!!!!!

A

O(1) VS O(N)
Summing function for a sorted, contiguous array of integers that starts with the number 1? Could easily be O(n) but…

const sumContiguousArray = function(ary){
//get the last item
const lastItem = ary[ary.length - 1]
//Gauss’s trick
return lastItem * (lastItem + 1) / 2
}
const nums = [1,2,3,4,5]
const sumOfArray = sumContiguousArray(nums

How well did you know this?
1
Not at all
2
3
4
5
Perfectly