i’ve recently come across an algorithms question involving a function which operates on two arrays and is defined like so:
function definition
i implemented the function by using two for loops and adding the values to a variable:
int f(int n, int m, int *a, int *b)
{
int x = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
x += a[i] * b[j] * (i + j + 2);
}
}
return x % 998244353;
}
the caveat though is that the question requires you to implement a solution that runs in linear time and i couldn’t figure out how to condense those two for loops into one evem after thinking on it for quite a bit of time.
does anyone have any suggestion?
kiwiclicker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.