I would like to get ideas for finding a fast way to get the maximal difference between 2 vectors as if they're accumulated .
For example, (not accumulated yet)
vector<int> vec1 = {10, 30, 20, 40 };
vector<int> vec2 = {5, 10, 5, 8 };
the naive way to get the result, is to accumulate them first into new vectors so:
vector<int> accumulated_vec1 = {10, 10+30, 10+30+20, 10+30+20+40};
vector<int> accumulated_vec2 = {5, 5+10, 5+10+5, 5+10+5+8};
i.e:
accumulated_vec1 = {10,40,60,100};
accumulated_vec2 = {5,15,20,28};
Then, the result is the max between abs(accumulated_vec1[i]-accumulated_vec2[i])
while 0 <= i <= 3.
so result = 72
(when i==3)
a faster way can be by representing the vectors by 1 number(even 0.10302040)... but I can't find it helpful :\ Think that I have millions pairs of the 2 vectors vec1 and vec2, and I am trying to avoid calculating the accumulated vectors for each of pair.. sorry if it's not clear but if I find a solution, I'll answer this annoying question.
.reserve()
on your accumulated vectors to avoid reallocating while you're pushing things back into it (I'm assuming you're filling it inside a loop in your actual code). If you do have a lot of elements like you just said, this would be a good optimization to your code. – notadamvector
s? Or what? – Barry