17 / 19

Why is accessing an array element O(1)?

Difficulty: 2/10

Constant-Time Array Access

Array access is O(1) because an array stores elements at predictable, contiguous memory addresses. Given the base address, element size, and index, the runtime can calculate the target element's address directly without scanning previous elements.

javascript
  1. 1

    Accessing A[0], A[100], or A[1,000,000] requires the same number of address calculations.

  2. 2

    There is no dependency on the number of preceding elements.

  3. 3

    Therefore, indexed read and update operations are O(1).

  4. 4

    This assumes ordinary array access rather than operations that first require searching for an index.

Follow-up Questions

  • Why is searching an unsorted array O(n)?
  • Why is insertion in the middle O(n)?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.