I have the following code. I have been told that the time complexity of this is O(n). But I am having a hard time understanding how. To me it seems like it is O(n) + O(n) + O(n) = O(3n) and the space complexity is O(3n) as well because 3 vectors that get populated to the size of n. Is my understanding correct ?
void productofself(std::vector<int> v)
{
int multipliedProduct = 0;
std::vector<int> left;
std::vector<int> right(v.size());
std::vector<int> result;
for (int i = 0; i < v.size(); i++)
{
if (i == 0)
{
multipliedProduct = 1;
}
else
{
multipliedProduct *= v[i - 1];
}
left.push_back(multipliedProduct);
}
for (int i = v.size() - 1; i >= 0; i--)
{
if (i == v.size() - 1)
{
multipliedProduct = 1;
}
else
{
multipliedProduct *= v[i + 1];
}
right[i]=multipliedProduct;
}
for (int i = 0; i < v.size(); i++)
{
result.push_back(left[i] * right[i]);
}
}