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

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 Correct Answer Ɵ(1)

Code:

#include<stdio.h>
int main() {
int ary = {250,1,9,7,3,4,5,21,15,19};
//if we take any 3 elemement from n distinct element in an array one of them would be neither maximum nor minimum.
int a = ary;
int b = ary;
int c = ary;
if((b > a && a >c) || (c > a && a > b)) 
printf("%d",a);
else if((a > b && b >c) || (c > b && b >a)) 
printf("%d",b);
else 
printf("%d",c);
}

Output: 9

9 is neither maximum nor minimum

Explanation:

Neither recursion nor iteration takes place in the above code, every statement in the above program takes a constant amount of time 

and hence order is θ (1)

Related Questions