• edited by
29,730 views
79 79 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)$

6 Answers

Best answer
145 145 votes

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

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

• edited by
23 23 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
4 4 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)
4 4 votes
We only need to consider any 3 elements and compare them. So the number of comparisons is constants, that makes time complexity as Θ(1) The catch here is, we need to return any element that is neither maximum not minimum. Let us take an array {10, 20, 15, 7, 90}. Output can be 10 or 15 or 20 Pick any three elements from given liar. Let the three elements be 10, 20 and 7. Using 3 comparisons, we can find that the middle element is 10

source:GEEKSFORGEEKS
4 4 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
0 0 votes

Take 1st three elements and compare them we can find our required elements this can be done on constant time 

So TC= O(1) 

Answer:
Position:
Show:

Related questions

34 34 votes
3 answers 3 answers
13.0k
13.0k views
go_editor asked Feb 12, 2015
12,979 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{Divid...
18 18 votes
2 answers 2 answers
9.0k
9.0k views
go_editor asked Feb 12, 2015
9,046 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...
60 60 votes
5 answers 5 answers
23.9k
23.9k views
go_editor asked Feb 13, 2015
23,899 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 ...
80 80 votes
5 answers 5 answers
28.9k
28.9k views
go_editor asked Feb 12, 2015
28,876 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...