GATE CS 2024 Set 1 — Question 17

MCQ+1 / -0.33EasyArray Operations (1D, 2D)Arrays & StringsProgramming & Data StructuresAsymptotic NotationsAsymptotic AnalysisAlgorithms

Algorithms → Arrays & Strings → Array Operations (1D, 2D)

Last updated

Question

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
A.
both O(N) and Ω(N)\Omega(N)
B.
O(N) but not Ω(N)\Omega(N)
C.
Ω(N)\Omega(N) but not O(N)
D.
neither O(N) nor Ω(N)\Omega(N)

Correct answer

(A) both O(N) and Ω(N)

Solution

To determine if an array of size NN is sorted (either ascending or descending), an algorithm must examine every element at least once to ensure no pair violates the sorting order. A single-pass algorithm that compares each element with its adjacent neighbor will perform exactly N1N-1 comparisons in the worst case (e.g., when the array is indeed sorted or the violation occurs at the very end).
Since the number of operations is proportional to NN, the time complexity is T(N)=Θ(N)T(N) = \Theta(N). By the definitions of asymptotic notation, f(n)=Θ(g(n))f(n) = \Theta(g(n)) implies that f(n)=O(g(n))f(n) = O(g(n)) and f(n)=Ω(g(n))f(n) = \Omega(g(n)). Therefore, the worst-case time complexity is both O(N) and Ω(N)\Omega(N).

More questions on Arrays & Strings

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free