StreakPeaked· Practice

ExamsGATETechnical

Given an integer array of size N, 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

  1. both O(N) and Ω(N)
  2. O(N) but not Ω(N)
  3. Ω(N) but not O(N)
  4. neither O(N) nor Ω(N)

Correct answer: both O(N) and Ω(N)

Solution

The algorithm checks each pair of adjacent elements in a single pass through the array, which requires examining all N elements, resulting in a linear time complexity of O(N). Since it must at least look at each element once to determine the order, it also has a lower bound of Ω(N), making the correct option both O(N) and Ω(N).

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →