data structures - Complexity of the algorithm that represnts the expression (n*(n-1)/2) -
for equation : (n(n-1))/2
if write algorithm represent equation, complexity of such algorithm?
(n*(n-1))/2
can represented (1 + 2 + ... + (n-1))
. finding sum using expanded expression have o(n)
time complexity , o(1)
space complexity.
if don't expand (n*(n-1))/2
sum-expression, takes o(1)
time complexity (n*(n-1))/2
.
why o(n)
expanded(sum) expression?
since going addition considering (n-1)
elements 1 one. o(n)
considered same o(n-1)
.
Comments
Post a Comment