There are three arrays a1, a2, a3 of size n. Function searches for common number in these arrays.
Algorithm is the next:
foreach n in a1
if n is found in a2
if n is found in a3
return true
return false
My guess that worse case will be the next: a1 and a2 are equal, a3 does not contain any common number with a1.
Complexity to iterate through array a1 will be O(i).
Complexity to search array a2 or a3 is f(n) (we do not know how they are searched).
My guess that overall complexity for worse case would be:
O(n) = n * f(n) * f(n) = n * (f(n))^2
I was told that that it is wrong.
What is correct answer then?
The correct answer for the given algorithm:
You don’t search
a3arrayf(n)times for eachnina1so you should use+instead of*.