Skip to content
Official PYQ · 2024

GATE CS 2024 Set 1 Q7 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Given an integer array of size NN, we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is

Choose one option
View worked solution

Answer: A

To determine if an array of size NN is sorted in either ascending or descending order, any algorithm must verify the relationship between all adjacent elements in the worst case.

Worst-Case Analysis:

  • The worst-case scenario occurs when the array is indeed fully sorted (either ascending or descending), or when the sorting order is violated only at the very last pair of elements.
  • In such cases, the algorithm must scan the entire array and perform comparisons for all adjacent pairs.
  • For an array of size NN, there are exactly N−1N - 1 adjacent pairs: (A[0],A[1]),(A[1],A[2]),…,(A[N−2],A[N−1])(A[0], A[1]), (A[1], A[2]), \dots, (A[N-2], A[N-1]).
  • Thus, the algorithm performs exactly N−1N - 1 comparisons in the worst case.

Since the number of operations in the worst case is proportional to NN, the worst-case time complexity T(N)T(N) is Θ(N)\Theta(N).

By the definitions of asymptotic notations:

T(N)=Θ(N)  ⟹  T(N)=O(N) and T(N)=Ω(N)T(N) = \Theta(N) \implies T(N) = \mathrm{O}(N) \text{ and } T(N) = \Omega(N)

Therefore, the worst-case time complexity is both O(N)\mathrm{O}(N) and Ω(N)\Omega(N).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions