edited by
18,032 views
62 votes
62 votes

An unordered list contains $n$ distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is

  1. $\Theta(n \log n)$
  2. $\Theta(n)$
  3. $\Theta(\log n)$
  4. $\Theta(1)$
edited by

6 Answers

Best answer
120 votes
120 votes

Correct Option: D – $\Theta(1)$

Because all elements are distinct, select any three numbers and output $2$nd largest from them.

edited by
20 votes
20 votes
they ask for an element which should not be minimum or maximum so We only need to consider any 3 elements and compare them. So the number of comparisons is constants, that makes time complexity as Θ(1)

Let us take an array {11, 20, 16, 7, 90}. Output can be 11 or 16 or 20 as

min = 7 and max = 90

Pick any three elements from given list. Let the three elements be 11, 20 and 7.

Using 3 comparisons, we can find that the middle element is 11.

meaning need to find middle element from 3 elements
3 votes
3 votes
Since all the elemets are distinct,taking any 3 elemets from array will guaratee that one element is smaller and one element is greater than this element.

Example : taking 3,2,1

here 2 is greater than 1 and smaller than 3.

This condition is true for any 3 elements of array. So choosing randomly 3 elements is sufficient to find an element which is neighter maximum nor minimum.

In worst cast out of choosen 3 elements one is maximum,one is minimum and 3rd is between them.So it is ensured in choosing only 3 elements.thus O(1)
3 votes
3 votes
It is bit clear that it requires O(1)

But if in question they mention that the list contains repetition of elements

Like 1 1 1 1 1 1 .since we can't get the element which is not min and max but we need to compare hole list then we require O(n)

Plz correct me if my logic is wrong
Answer:

Related questions

7.2k
views
3 answers
26 votes
go_editor asked Feb 12, 2015
7,240 views
Given below are some algorithms, and some algorithm design paradigms.$$\begin{array}{ll|ll}\hline \text{1.} & \text{Dijkstra's Shortest Path} & \text{i.} & \text{Divide a...
6.7k
views
2 answers
16 votes
go_editor asked Feb 12, 2015
6,662 views
Consider two decision problems $Q_1, Q_2$ such that $Q_1$ reduces in polynomial time to 3-SAT and 3-SAT reduces in polynomial time to $Q_2$. Then which one of the followi...
14.9k
views
4 answers
48 votes
go_editor asked Feb 13, 2015
14,931 views
In a connected graph, a bridge is an edge whose removal disconnects the graph. Which one of the following statements is true?A tree has no bridgesA bridge cannot be part ...
17.2k
views
3 answers
58 votes
go_editor asked Feb 12, 2015
17,210 views
Consider a processor with byte-addressable memory. Assume that all registers, including program counter (PC) and Program Status Word (PSW), are size of two bytes. A stack...