I\'m working through some practice problems where I\'m given a target time complexity and space complexity. One of them gives a target time complexity of O(N+M). I\'m having som
A simple example of algorithm that is O(m+n)
:
int sum(int[] nArr, int[] mArr) {
int sum = 0;
for(int i : nArr) {
sum += i;
}
for(int i : mArr) {
sum += i;
}
return sum;
}
To compute the sum, you need to go through all elements in nArr
(size n
) and all elements in mArr
(size m
), so the overall complexity is O(m+n)