Consider an array 𝐴 of integers of size 𝑛. The indices of 𝐴 run from 1 to…

2026

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?

  1. A.

    Θ(n)

  2. B.

    Θ(log⁑n)

  3. C.

    Θ(nlog⁑n)

  4. D.

    Θ(n2)

Attempted by 78 students.

Show answer & explanation

Correct answer: A

The given condition requires checking if the difference between consecutive elements is strictly increasing for all valid indices.

Let D[i] = A[i+1] - A[i]. The condition becomes D[i] > D[j] for all i > j, which implies the sequence D must be strictly increasing.

We can compute all D values and verify the increasing order in a single pass through the array. This involves O(n) operations to calculate differences and O(n) to check the order.

Thus, the total time complexity is Θ(n), as we only need to traverse the array once to validate the property.

Explore the full course: Gate Guidance By Sanchit Sir