Skip to content
Official PYQ · 2000

GATE CS 2000 Q1.15 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Let SS be a sorted array of nn integers. Let T(n)T(n) denote the time taken for the most efficient algorithm to determined if there are two elements with sum less than 10001000 in SS. Which of the following statement is true?

Choose one option
View worked solution

Answer: A

Since the array SS of nn integers is already sorted in non-decreasing order, we have:

S[0]≤S[1]≤S[2]≤⋯≤S[n−1]S[0] \le S[1] \le S[2] \le \dots \le S[n-1]

To determine if there exist any two elements in SS whose sum is less than 10001000, we only need to look at the two smallest elements in the array, which are S[0]S[0] and S[1]S[1]:

  1. Case 1 (n<2n < 2): It is impossible to find two elements, so the answer is immediately false. This takes O(1)O(1) time.
  2. Case 2 (n≥2n \ge 2): The minimum possible sum of any two distinct elements in SS is S[0]+S[1]S[0] + S[1].
    • If S[0]+S[1]<1000S[0] + S[1] < 1000, then we have found two elements whose sum is less than 10001000.
    • If S[0]+S[1]≥1000S[0] + S[1] \ge 1000, then for any other pair of indices i≠ji \neq j, we have S[i]+S[j]≥S[0]+S[1]≥1000S[i] + S[j] \ge S[0] + S[1] \ge 1000. Thus, no such pair can exist.

Therefore, the problem reduces to checking if S[0]+S[1]<1000S[0] + S[1] < 1000. Since array indexing, addition, and comparison are basic operations that take constant time, the most efficient algorithm runs in O(1)O(1) time.

Thus, T(n)T(n) is O(1)O(1).

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

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