I want to normalize elements in an array. The solution I found is to take the sum of the elements and divide each element with this total. Can you find more efficient way of doing this?
The runtime I achieve is 2N. Any better idea?
normalize(array)
{
sum = 0;
for i = 0 to array.size() do
sum += array[i];
end
for i = 0 to array.size() do
array[i] /= sum;
end
}