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 , 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 B.
O(N) but not C.
but not
O(N)D.
neither
O(N) nor Correct answer
(A) both O(N) and Ω(N)
Solution
To determine if an array of size 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 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 , the time complexity is . By the definitions of asymptotic notation, implies that and . Therefore, the worst-case time complexity is both
O(N) and .More questions on Arrays & Strings
2024 Set 2 Q13Consider the following C program. Assume parameters to a function are evaluated from right to left.…2024 Set 2 Q17Let be the adjacency matrix of a simple undirected graph . Suppose is its own inverse.…2024 Set 1 Q18Consider the following C program: [code] Which one of the following statements is CORRECT?2024 Set 1 Q19Consider the following C program: [code] Assume that the input to the program from the command line…2024 Set 1 Q21In a tree, the requirement of at least half-full (50%) node occupancy is relaxed for which…
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free