Question:hard

Consider an array 𝐴 of integers of size 𝑛. The indices of 𝐴 run from 1 to 𝑛. An
algorithm is to be designed to check whether 𝐴 satisfies the condition given below.
βˆ€π‘–, π‘—βˆˆ{1, … , π‘›βˆ’1} such that 𝑖> 𝑗, (𝐴[𝑖+ 1] βˆ’π΄[𝑖]) > (𝐴[𝑗+ 1] βˆ’π΄[𝑗])
Which one of the following gives the worst case time complexity of the fastest
algorithm that can be designed for the problem?

Show Hint

The condition says the difference sequence \(d[k]=A[k+1]-A[k]\) is strictly increasing. This only requires checking consecutive pairs, doable in one linear scan, giving \(\Theta(n)\).
Updated On: Aug 3, 2026
  • Θ(𝑛)
  • Θ(log(𝑛))
  • Θ(𝑛 log(𝑛))
  • \(\Theta(n^2)\)
Show Solution

The Correct Option is A

Solution and Explanation

Step 1: Rewrite the condition using consecutive differences. Let \(d[k] = A[k+1]-A[k]\) for \(k=1\) to \(n-1\). The problem asks us to verify that whenever one index exceeds another, its difference value also exceeds the other's difference value -- in other words, the array of differences must be sorted in strictly ascending order.
Step 2: A naive approach would test every pair \((i,j)\) with \(i > j\), giving \(O(n^2)\) comparisons, but this ignores the transitive nature of the 'less than' ordering.
Step 3: Because 'ascending order' is a transitive property, testing only adjacent pairs \(d[k] < d[k+1]\) for \(k = 1, \dots, n-2\) is both necessary and sufficient. If any adjacent pair fails, the sequence is not ascending and the original condition fails for some \((i,j)\); if all adjacent pairs pass, ascending order (and hence the original condition) is guaranteed for every pair by chaining inequalities.
Step 4: Both computing the \(n-1\) differences and checking the \(n-2\) adjacent comparisons take linear time, so the whole check runs in \(O(n)\).
Step 5: Since reading every element of the input array is unavoidable to guarantee correctness in the worst case (an adversary could hide a violation at any single position), \(\Omega(n)\) is also a lower bound.
Step 6: The matching upper and lower bounds pin the worst case complexity of the optimal algorithm at \(\Theta(n)\).
Final Answer: \(\Theta(n)\)
Was this answer helpful?
0

Top Questions on Algorithm design techniques


Questions Asked in GATE CS exam