Analysing the time complexity of Heap
Anonymous User
202

Question:
If you have an empty priority queue to which you want to add n items, one at a time, what will be the time complexity ?

void find_complexity(vector<int> A, vector<int> B){
	// Let A and B be int array of size n
	priority_queue<int> pq;
	for(int i = 0; i < n; ++i){
		pq.push(A[i] + B[i]);
	}
}

// What will be the complexity of the for loop in this case

Solution:
Each push opertion takes log(n) time and we need to insert n elements.
so complexity should be log1 + log2 + ... log(n) = log(1.2...n) = log(n!) ≈ log(n^n) = nlog(n)

Therefore the complexity is O(nlogn).

Comments (1)