I have the following code and am trying to figure out its time complexity:
int sum(int m, int n, int K) {
int s = 0;
for (int i = 0; i < n; i++) {
s += i;
if (i == K % 2) {
for (int j = 0; j < m; j++) {
s += j;
}
}
}
return s;
}
According to the code, the outer loop runs at O(n) and the inner loop runs at O(m). However, the inner loop is executed only once (when i is equal to K % 2). I'm not sure whether the time complexity when it only run once will be O(nm) or O(n + m). Currently, I doubt that the complexity should be O(n + m). Can someone explain this case for me?