Data Structures & AlgorithmsVery hardProduct-company MCQ pattern
What is the minimum number of comparisons needed, in the worst case, to find BOTH the maximum and the minimum of 100 distinct numbers?
- A. 150
- B. 100
- C. 198
- D. 148
Correct answer: D. 148
Compare elements in pairs (50 comparisons), then find the max among the 50 winners (49) and the min among the 50 losers (49): 50 + 49 + 49 = 148 = ⌈3n/2⌉ − 2. Separate scans for max and min need 2(n − 1) = 198.
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
- One Lomuto partition (pivot = last element, i marks the end of the 'smaller' part, swap when a[j] < pivot) is applied to [7, 2, 9, 4, 3…
- Insertion sort (ascending) is applied to [5, 2, 4, 6, 1, 3]. What does the array look like after the first 3 passes (i.e. after inserting…
- Among insertion sort, merge sort, heap sort, quick sort, bubble sort and selection sort (standard array versions), how many are BOTH…
- LSD radix sort (base 10, stable counting sort per digit) is applied to [170, 45, 75, 90, 802, 24, 2, 66]. What is the array after the…
- What does this function return for a = [1, 3, 3, 3, 5, 7] with x = 3 and with x = 4? python def g(a, x): lo, hi = 0, len(a) - 1 while lo <…
- while (lo <= hi) binary search runs on a sorted array of 31 elements for a key LARGER than every element. How many times does the loop…