What is the worst-case running time of this algorithm when the input vector nums has size šn?C++12345678910boolĀ containsDuplicate(constĀ std::vector<int>&Ā nums)Ā {Ā Ā Ā Ā std::set<int>Ā seen;Ā Ā Ā Ā forĀ (intĀ xĀ :Ā nums)Ā {Ā Ā Ā Ā Ā Ā Ā Ā ifĀ (seen.contains(x))Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā returnĀ true;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā seen.insert(x);Ā Ā Ā Ā }Ā Ā Ā Ā returnĀ false;}Ī(logā”š)Ī(logn)Ī(š2)Ī(n 2 )Ī(šlogā”š)Ī(nlogn)Ī(š)Ī(n)Submit
Question
What is the worst-case running time of this algorithm when the input vector nums has size šn?C++12345678910boolĀ containsDuplicate(constĀ std::vector<int>&Ā nums)Ā {Ā Ā Ā Ā std::set<int>Ā seen;Ā Ā Ā Ā forĀ (intĀ xĀ :Ā nums)Ā {Ā Ā Ā Ā Ā Ā Ā Ā ifĀ (seen.contains(x))Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā returnĀ true;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā seen.insert(x);Ā Ā Ā Ā }Ā Ā Ā Ā returnĀ false;}Ī(logā”š)Ī(logn)Ī(š2)Ī(n 2 )Ī(šlogā”š)Ī(nlogn)Ī(š)Ī(n)Submit
Solution
The worst-case running time of this algorithm when the input vector nums has size š is Ī(šlogš).
Here's why:
The algorithm uses a set to keep track of the elements it has seen. In C++, std::set is typically implemented as a self-balancing binary search tree. The contains and insert operations on a set both have a time complexity of O(log n) in the worst case.
The algorithm performs these operations for each of the n elements in the input vector. Therefore, the total time complexity in the worst case is O(n log n).
Similar Questions
What is the worst case time complexity of the following code :/* * V is sorted * V.size() = N * The function is initially called as searchNumOccurrence(V, k, 0, N-1) */int searchNumOccurrence(vector<int> &V, int k, int start, int end) { if (start > end) return 0; int mid = (start + end) / 2; if (V[mid] < k) return searchNumOccurrence(V, k, mid + 1, end); if (V[mid] > k) return searchNumOccurrence(V, k, start, mid - 1); return searchNumOccurrence(V, k, start, mid - 1) + 1 + searchNumOccurrence(V, k, mid + 1, end);}NOTE : This question involves recursion which will be explained later in topic Backtracking. So, if you are not able to approach this question now, you can give it a try later.
What is the worst case time complexity of the following code:int memo[101][101];int findMinPath(vector<vector<int> >& V, int r, int c) { int R = V.size(); int C = V[0].size(); if (r >= R || c >= C) return 100000000; // Infinity if (r == R - 1 && c == C - 1) return 0; if (memo[r][c] != -1) return memo[r][c]; memo[r][c] = V[r][c] + min(findMinPath(V, r + 1, c), findMinPath(V, r, c + 1)); return memo[r][c];}Callsite : memset(memo, -1, sizeof(memo));findMinPath(V, 0, 0);Assume R = V.size() and C = V[0].size() and V has positive elements
What is the time complexity of Binary Search in the worst case?
Worst case efficiency of binary search is
In which case does the binary search algorithm perform the worst? Question 17Select one: When the array contains duplicate elements When the element is not present in the array When the array is already sorted When the element is at the middle of the array
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.