Data Structures & AlgorithmsVery hardGATE CS · Product placement
An array holds n distinct elements. What is the minimum number of comparisons that ANY comparison-based algorithm needs, in the worst case, to find the second-largest element?
- A. 2n − 3
- B. n + ⌈log₂ n⌉ − 2
- C. ⌈3n/2⌉ − 2
- D. n − 1
Correct answer: B. n + ⌈log₂ n⌉ − 2
A knockout tournament finds the maximum in n − 1 comparisons; the second largest lost directly to the maximum, and there are at most ⌈log₂ n⌉ such candidates, needing ⌈log₂ n⌉ − 1 more. ⌈3n/2⌉ − 2 is the bound for finding both max and min.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [35, 8…
- Keys 107, 29, 82, 81, 18, 115, 85 are inserted in this order into an empty table of size 17 using quadratic probing: probe i goes to (k…
- Keys 80, 25, 99, 55, 30, 13, 57, 42 are inserted in this order into an empty table of size 13 with h(k) = k mod 13 and linear probing…
- A queue is implemented with two stacks (push to IN; pop from OUT, refilling OUT from IN only when OUT is empty). Which statement is correct?
- ^ is exponentiation (highest precedence, right-associative); and / come next; + and − are lowest; others are left-associative. What is the…
- Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is…