GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Reduce global order checks to adjacent comparisons

2 questions solved the same way, newest first. Back to Algorithms

How these are solved

A sequence is ordered iff successive entries satisfy the local order condition.2024 checks the input itself in either direction;2026 checks strict increase of successive differences, reducing universal pair inequalities to adjacent difference comparisons.

2 marks · MCQ
Report issue

Checking increasing adjacent differences

Correctness, Invariants & Design

Question 38, GATE 2026: For integer A[1…n], determine whether for every i>j in {1,…,n−1}, A[i+1]−A[i] > A[j+1]−A[j]. Choose optimal worst-case time: A Θ(n); B Θ(log n); C Θ(n log n); D Θ(n²).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

For integer A[1…n], determine whether for every i>j in {1,…,n−1}, A[i+1]−A[i] > A[j+1]−A[j]. Choose optimal worst-case time: A Θ(n); B Θ(log n); C Θ(n log n); D Θ(n²).

Approach

Reduce all pairwise comparisons to adjacent comparisons using transitivity.

Step-by-step solution

1. Define d_i=A[i+1]−A[i]. The condition says each later difference is larger than every earlier one. This is equivalent to d_1<d_2<…<d_(n−1).
2. The forward implication includes all adjacent pairs. Conversely, if adjacent inequalities hold, chaining d_j<d_(j+1)<…<d_i proves the required inequality for every i>j.
3. Algorithm: if n≤2 return true; set previous=A[2]−A[1]; for i=2,…,n−1 set current=A[i+1]−A[i]; if current≤previous return false; otherwise set previous=current. If the loop ends return true. Only O(1) extra storage is needed.
4. The loop uses O(n) time. For a lower bound, consider a valid array such as A[i]=i², whose differences are strictly increasing. If any position is unread, an adversary can change that entry sufficiently to violate a neighboring comparison while leaving all inspected values unchanged. Thus worst-case inspection requires Ω(n) entries for n≥3.
5. The optimal bound is Θ(n), A. Use sufficiently wide arithmetic so subtraction does not overflow; the proof is in the mathematical-integer model.

Original paper, page 25 · Answer key

1 mark · MCQ
Report issue

Upper and lower bounds for a single pass

Searching, Sorting & Selection

Question 17, GATE 2024: An algorithm checks whether an N-element integer array is sorted in either ascending or descending order by one pass comparing adjacent elements. Its worst-case running time is: A both O(N) and Ω(N); B O(N) but not Ω(N); C Ω(N) but not O(N); D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Approach: Big-O and big-Omega describe upper and lower bounds on the same worst-case running-time function; they are not synonyms for worst case and best case.
1. Maintain whether any adjacent pair increases and whether any adjacent pair decreases. Each of the N−1 adjacent pairs requires only constant work. The single pass therefore takes at most cN+d operations, giving O(N).
2. On an already sorted array, no early contradiction occurs. The algorithm must examine through the final adjacent pair to certify the property. Such an input requires a number of comparisons proportional to N, giving a worst-case Ω(N) lower bound.
3. Together these bounds give Θ(N).
Answer: A. Some unsorted inputs may allow early termination, but that does not remove the linear worst-case lower bound.

Original paper, page 11 · Answer key