The Gateway to Computer Science Excellence
+10 votes

Given a set of $n$ distinct numbers, we would like to determine both the smallest and the largest number. Which of the following statements is TRUE?

  1. These two elements can be determined using $O\left(\log^{100}n\right)$ comparisons.
  2. $O\left(\log^{100}n\right)$ comparisons do not suffice, however these two elements can be determined using $n + O(\log n)$ comparisons.
  3. $n+O(\log n)$ comparisons do not suffice, however these two elements can be determined using $3\lceil n/2 \rceil$ comparisons.
  4. $3\lceil n/2 \rceil$ comparisons do not suffice, however these two elements can be determined using $2(n - 1)$ comparisons.
  5. None of the above.
in Algorithms by Boss (30.8k points) | 1.4k views
suppose n = 1024, exact comparisons required will be $1024*1.5 - 2 = 1534$ (C) is the correct option.

What is the explanation for it @Manu Thakur.

The divide and conquer algorithm for finding the min and max elements of a list operates in worst case $3n/2 - 2$ comparisons. It is described in Sartaj Sahni's algorithms book.

2 Answers

+10 votes
Best answer

I think answer will be C.

To be accurate, it will need $3n/2 -2$ comparisons .

by Boss (10.5k points)
edited by

for even numbers- 1.5n-2

for odd- 3/2(n-1) Comparisions


Why n+O(logn) doesn't suffice?
+5 votes

Similar to the approach proposed by Himanshu1 here:

Construct a decision tree to determine the minimum element: $n - 1$ comparisons

Maximum element can be found from the same tree as it will be the biggest element out of $\frac{n}{2}$ elements at the first level which lost the decision: $\frac{n}{2} - 1$ comparisons

Therefore, the resultant number of comparisons: $3(\frac{n}{2}) - 2$, tighest bound on which is option (C).

by Active (1.8k points)
@pranav Kant Gaur

Maximum element will at leaf na? and  at leaf there are n elements so comparison should be n-1. Please clear my doubt.


Yes maximum comparison will be for leaf.But question is asking for both maximim and minimum .

For maximum n-1 comparison

For minimum n-1 comparison

Total number of comparison=2*(n-1)

If you will draw the tree you will find that the leaf node level(is the last level) needs n/2 comparison.And it is common to both minimum and maximum.

So total number of comparison required=2*(n-1) -n/2 =(3/2)n-2 when n is even.


This is a rather good explanation.


Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,292 answers
104,909 users