edited by
2,181 views
17 votes
17 votes

Let $S=\left \{ x_{1},....,x_{n} \right \}$ be a set of $n$ numbers. Consider the problem of storing the elements of $S$ in an array $A\left [ 1...n \right ]$ such that the following min-heap property is maintained for all $2\leq i\leq n: A [\lfloor i/2\rfloor]\leq A[i]$. (Note that $\lfloor x\rfloor$ is the largest integer that is at most $x$). Which of the following statements is TRUE?

  1. This problem can be solved in $O(\log n)$ time.
  2. This problem can be solved in $O (n)$ time but not in $O(\log n)$ time.
  3. This problem can be solved in $O(n\log n)$ time but not in $O (n)$ time.
  4. This problem can be solved in $O \left ( n^{2} \right )$ time but not in $O(n\log n)$ time.
  5. None of the above.
edited by

2 Answers

Best answer
27 votes
27 votes

Store the elements in an array and then call build_heap(A). the build_heap takes $O(n)$ time.

So, option 'b' is correct.


But, if we try building heap by inserting each element one by one, the total complexity will be then $O(n \log n)$. Because insertion takes $O(\log n)$ and inserting '$n$' elements will take $O(n \log n)$.

edited by
1 votes
1 votes
As per the given question statement,we can take all the n numbers and build a min-heap in O(n) time. For maintaining the min-heap property, we will do balancing in O(log n) time. Hence total time =O (n +log n). =O(n )
Answer:

Related questions

4.2k
views
3 answers
17 votes
makhdoom ghaya asked Oct 25, 2015
4,245 views
The first $n$ cells of an array $L$ contain positive integers sorted in decreasing order, and the remaining $m - n$ cells all contain 0. Then, given an integer $x$, ... $O (\log (m / n))$ comparisons suffice.
8.0k
views
4 answers
39 votes
makhdoom ghaya asked Oct 22, 2015
8,004 views
Given a set of $n=2^{k}$ distinct numbers, we would like to determine the smallest and the second smallest using comparisons. Which of the ... determine these two elements.$nk$ comparisons are necessary to determine these two elements.
1.9k
views
1 answers
7 votes
makhdoom ghaya asked Oct 25, 2015
1,936 views
Given an integer $n\geq 3$, consider the problem of determining if there exist integers $a,b\geq 2$ such that $n=a^{b}$. Call this the forward problem. The ... $NP$-hard.Both the forward and reverse problem are $NP$-hard.None of the above.
3.0k
views
3 answers
15 votes
makhdoom ghaya asked Oct 24, 2015
2,981 views
Let $G$ be a connected simple graph (no self-loops or parallel edges) on $n\geq 3$ ... present in any maximum weight spanning tree.$G$ has a unique maximum weight spanning tree.