If I want to get the sum of a range [0-n], where n starts at 1 and increments up to n as the max number in the array, just memorize this equation. This is an O(1) solution, which is ideal.
sum = n x (n + 1) // 2
For example, the series sum for range [0-4] is 10.
1 +
1 + 1 +
1 + 1 + 1 +
1 + 1 + 1 + 1 = 10
4 x (4 + 1) // 2 = 10
You'll probably encounter this in a leetcode problem.
Pressing this button removes the post and it can't be undone:
Delete
A quote from someone that was funny - Author
See Vods