What is the time complexity of accessing an element at a given index in an array?

Options

  • A. O(1)
  • B. O(n)
  • C. O(log n)
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. O(1)

Detailed Explanation

Accessing an array element by its index generally takes O(1) constant time. The computer calculates the memory address using the base address, element size, and index. For example, if A starts at a known address and each element occupies the same amount of memory, the address of A[i] can be calculated directly. This is called random access and is an important advantage of arrays over linked lists.