C++ Consider the following code: 01: list folderol (int n) 02: { 03: unordered_s
ID: 3737269 • Letter: C
Question
C++
Consider the following code:
01: list folderol (int n)
02: {
03: unordered_set t, u;
05:
06: while (t.size() < n)
07: t.insert (rand() % (n*n));
08: while (u.size() < n)
09: u.insert (rand() % (n*n));
10:
11: list L;
11: for (int i: t)
12: if (u.count(i) > 0)
13: L.push_back(i);
14: return L;
15: }
rand() returns a random non-negative integer and runs in `O(1)` time. Assume that each possible unsigned int value is equally likely to be returned on any given call to rand().
Match the line numbers below to the closest correct complexity expression, giving the single-letter identifier for your chosen complexity.
A: O(1)
B: O(log n)
C: O(sqrt(n))
D: O(n)
E: O(n log n)
F: O(n sqrt(n))
G: O(n2)
H: O(n2 log(n))
I: O(n2 sqrt(n))
J: O(n3)
K: none of these
If the line numbers are preceded by "Avg", give the average case complexity. Otherwise, give the worst case complexity. Line 7:
Avg Line 7:
Lines 6-7:
Line 13:
Line 12:
Avg Line 12:
Lines 11-13:
Avg Lines 11-13:
Line 14:
Avg Line 14:
Explanation / Answer
Line 7: linear in container size i.e. D
Avg Line 7: constant.i.e. A
Lines 6-7: linear in the number of elements inserted. i.e. D
Line 13: N*(size+1): number of elements inserted times the container size plus one. since before insertion the size of container is 0. so ans = D
Line 12:linear in container size. i.e. D
Avg Line 12: A
Lines 11-13: G
Avg Lines 11-13: D
Line 14:D
Avg Line 14: D ( in both the case all values of the list will be copied to another calling function)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.