0
Explore
0

Index formula derivation for 1-D, 2-D, 3-D and n-D Array

Updated on March 1, 2026

An array is a fundamental data structure in programming used to store collections of elements of the same data type. Every element in an array is accessed using an index, and behind the scenes, programming languages use index formulas to convert multidimensional array positions into a single linear memory address.

We will derive the index formulas for:

  • 1-D arrays
  • 2-D arrays
  • 3-D arrays
  • n-D arrays

And We assume:

What Is Array Indexing?

Array indexing is the method used to locate an element in memory using its position.

Because arrays store data in contiguous memory, the index formula helps:

  • Access elements in O(1) time
  • Convert logical positions (row, column, plane) into physical memory locations

1-D Array Index Formula

A one-dimensional array stores its elements in a straight line, using continuous (contiguous) memory locations. Because of this, each element can be accessed directly using its index, without scanning the entire array.

Index Formula of 1-D Array

index = i

Where:

  • i is the position of the element (starting from 0)

Key Points

  • Elements are stored sequentially
  • No complex calculation is required
  • Direct mapping between index and memory offset

Example of 1D Array

Consider the array:

A[5] = {a, b, c, d, e}

Accessing elements:

IndexElement
A[0]a
A[1]b
A[2]c

So, A[2] directly retrieves the third element c, because the elements are stored in a continuous block of memory.

2-D Array Index Formula (Row-Major Order)

A two-dimensional array is organized as rows and columns. It can be imagined as a matrix made up of rows and columns. Internally, it is still stored as one continuous block of memory.

To access an element, you need two indices:

  • one for the row, and
  • one for the column.

Indexing Formula (Row-Major Order)

index = (i × column_count) + j

Where:

  • i = row index (starting from 0)
  • j = column index (starting from 0)
  • column_count = total number of columns in the array

Why This Works

  • i × column_count skips complete rows
  • + j moves to the exact column

Example

Consider the array

B[3][4]   // 3 rows, 4 columns

To find the position of B[1][2] (row 1, column 2):

Calculation:

index = (1 × 4) + 2
= 6

So, B[1][2] is stored at index 6 in the linear memory representation.

3-D Array Index Formula

A three-dimensional array can be visualized as a stack of 2D matrices (planes). Accessing an element requires three indices:

  • one for the plane,
  • one for the row, and
  • one for the column.

Indexing Formula (Row-Major Order)

index = (i × plane_size) + (j × column_count) + k

Where:

  • i = plane index (starting from 0)
  • j = row index within the plane
  • k = column index within the row
  • plane_size = row_count × column_count (total elements per plane)
  • column_count = total number of columns in each row

Example

Consider the array

C[2][3][4]   // 2 planes, 3 rows, 4 columns

C[2][3][4] → 2 planes, 3 rows, 4 columns

  • plane_size = 3 × 4 = 12
  • column_count = 4

To find the position of C[1][2][3]:

Calculation:

index = (1 × 12) + (2 × 4) + 3
       = 12 + 8 + 3
       = 23

So, C[1][2][3] is stored at index 23 in linear memory.

n-D Array Index Formula

An n-dimensional array generalizes the ideas from 1D, 2D, and 3D arrays. Instead of rows, columns, or planes, it may have many dimensions. Accessing an element requires one index for each dimension. n-dimensional array extends the same logic used in 2-D and 3-D arrays.

General Indexing Formula (Row-Major Order)

index = Σ (iₖ × Sₖ), for k = 1 to n

Where:

  • iₖ = index in the k-th dimension (zero-based)
  • Sₖ = stride of the k-th dimension

What Is a Stride?

The stride Sₖ is the number of memory positions you must skip to move 1 step forward in the k-th dimension.

Stride Formula

Sₖ = dimₖ₊₁ × dimₖ₊₂ × ... × dimₙ

For the last dimension,

Sₙ = 1

n-D Array Example

Consider a 4-D array:

D[10][20][30][40]

Stride Calculation

  • S₁ = 20 × 30 × 40 = 24,000
  • S₂ = 30 × 40 = 1,200
  • S₃ = 40
  • S₄ = 1

To find the position of D[i][j][k][l], apply:

Index Formula

index = (i × 24,000) + (j × 1,200) + (k × 40) + l

This gives the exact linear memory location of the element in row-major order.

Why Index Formulas Are Important?

Understanding index derivation helps in:

  • Cracking DSA and interview questions
  • Writing efficient algorithms
  • Optimizing cache-friendly memory access
  • Working with matrices, tensors, and ML data
  • Understanding compiler and OS memory behavior

Quick Comparison Table

Array TypeIndex Formula
1-Di
2-D(i × columns) + j
3-D(i × plane_size) + (j × columns) + k
n-DΣ (iₖ × Sₖ)

Conclusion

Understanding array indexing formulas is essential for:

  • Efficient memory access and traversal
  • Performance optimization in large-scale computations
  • Working with complex data structures like matrices, tensors, and multidimensional grids

Whether you’re working with simple 1D arrays or high-dimensional datasets, these formulas translate logical positions into memory addresses, forming the foundation for array manipulation in programming.