How to find sum of elements from given index interval (i, j) in constant time?
Posted
by Rajendra
on Stack Overflow
See other posts from Stack Overflow
or by Rajendra
Published on 2010-03-18T20:23:02Z
Indexed on
2010/03/18
20:31 UTC
Read the original article
Hit count: 112
Given an array. How can we find sum of elements in index interval (i, j)
in constant time. You are allowed to use extra space.
Example:
A: 3 2 4 7 1 -2 8 0 -4 2 1 5 6 -1
length = 14
int getsum(int* arr, int i, int j, int len);
// suppose int array "arr" is initialized here
int sum = getsum(arr, 2, 5, 14);
sum should be 10 in constant time.
© Stack Overflow or respective owner