Having a difficult time grasping big O when it comes to printing out the common elements in two arrays. On of size a, the other of size b.
I know two unsorted arrays is O(ab)
But what about when a is unsorted and b is sorted?
When both are sorted?
Any explanation would be great.
An efficient way to do this is to use a hash table ( HashMap
in Java). An algorithm is:
foreach element of the smallest array O(N) add element to the hash table O(1) foreach element of the biggest array O(M) if element is in the hash table O(1) print element
The time complexity of every step is annotated in the pseudo code above.
So the overall time complexity is O(N + M) , and the space complexity is O(N)
Note that
Hope that helps!
I'm assuming that you want to use $O(1)$ additional memory, because otherwise, you could solve either problem (sorted or unsorted) in $O(a + b)$ by using a hash table. Also, I'm going to assume the elements in each array are unique, but the algorithms can easily be mended if not.
If both are unsorted, you can in fact improve on your approach by simply sorting one of the arrays, then using the approach below. This gives $O((a+b)\\min(\\lg a, \\lg b)$.
If a is unsorted and b is sorted, you can iterate for all elements in a, and check if they're in b by using binary search . This gives an $O(b + a\\lg b)$ approach.
If both are sorted, you can easily get $O(a + b)$ by using a simple two-pointer algorithm (successively increment indexes on both arrays).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.