Problem Statement: Design and implement a data structure that can store all the different numbers from two separated streams. Implement two methods 1) intersection() 2) union()
Below is my take,
Time Complexity intersection method:
Let's say Set has N elements.
- for loop, O(N)
- contains and add method, O(1)
O(N) + O(1) = O(N)
Time Complexity union method:
Let's say Set1 has N elements and Set2 has M elements.
- Set<Integer> union = new HashSet<Integer>(s1); O(N)
- union.addAll(s2); O(M)
O(N) + O(M) = O(N + M) Is this correct?
Below is my take,
Time Complexity intersection method:
Let's say Set has N elements.
- for loop, O(N)
- contains and add method, O(1)
O(N) + O(1) = O(N)
Time Complexity union method:
Let's say Set1 has N elements and Set2 has M elements.
- Set<Integer> union = new HashSet<Integer>(s1); O(N)
- union.addAll(s2); O(M)
O(N) + O(M) = O(N + M) Is this correct?