Skip to main content

GATE CSE 2019Algorithms › Growth of Functions & Complexity AnalysisVerified

GATE CSE 2019 PYQ: There are n n n unsorted arrays: A 1 , A 2 , … , A n A_1, A_2, \dots, A_n A 1 ​ , A 2 ​ , … , A n ​ . Assume…

There are nn unsorted arrays: A1,A2,…,AnA_1, A_2, \dots, A_n. Assume that nn is odd.Each of A1,A2,…,AnA_1, A_2, \dots, A_n contains nn distinct elements. There are no common elements between any two arrays. The worst-case time complexity of computing the median of the medians of A1,A2,…,AnA_1, A_2, \dots , A_n is

  1. A
    O(n)O(n)
  2. B
    O(n log⁡ n)O(n \: \log \: n)
  3. C
    O(n2)O(n^2)
    Correct answer
  4. D
    Ω(n2log⁡n)\Omega (n^2 \log n)

Answer

(C) O ( n 2 ) O(n^2) O ( n 2 )

Approach

The task decomposes into: (1) find the median of each of the n unsorted arrays, each of size n; (2) collect these n medians into a new list; (3) find the median of that list. Since each array can be processed independently, use a linear-time deterministic selection algorithm for each array, and then for the final list.

The fastest known deterministic method finds each array's median in O(n) time, so n arrays take $O(n^2)$. The final median of the n medians costs only O(n), giving overall $O(n^2)$. Option C is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.