MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / PrefixSum

Method PrefixSum

structure/fenwicktree/fenwicktree.go:37–46  ·  view source on GitHub ↗

PrefixSum returns the sum of the prefix ending at position pos.

(pos int)

Source from the content-addressed store, hash-verified

35
36// PrefixSum returns the sum of the prefix ending at position pos.
37func (f *FenwickTree) PrefixSum(pos int) int {
38 if pos > f.n {
39 return 0
40 }
41 prefixSum := 0
42 for i := pos; i > 0; i -= (i & -i) {
43 prefixSum += f.bit[i]
44 }
45 return prefixSum
46}
47
48// RangeSum returns the sum of the elements in the range l to r
49// both inclusive.

Callers 2

RangeSumMethod · 0.95
TestFenwickTreeFunction · 0.95

Calls

no outgoing calls

Tested by 1

TestFenwickTreeFunction · 0.76