liveBook · Manning

Modulo arithmetic

As explained in Grokking Data Structures

Modular arithmetic is arithmetic in which values are considered equivalent when they have the same remainder under a specified modulus.

Modulo arithmetic uses the remainder after division to map a continuing sequence back onto a fixed set of positions. In a circular queue, this lets virtual indexes wrap from an array’s end to its beginning. section 9.7

Wrapping an eight-element array

For an array indexed from 0 to 7, 8 % 8 equals 0, 9 % 8 equals 1, and 10 % 8 equals 2. Thus, virtual index 8 refers to array index 0, allowing the rear pointer to continue after the array’s end. section 9.7

How Grokking Data Structures covers it

How deep it goes
Defines remainders through an eight-element array example and stops short of broader mathematical treatment.
Examples
Uses 8 % 8 = 0, 9 % 8 = 1, and virtual index 8 wrapping to array index 0.
What it assumes
Assumes array indexing and division while explaining modulo through a concrete eight-element queue.
What you can do after
Apply it via the modulo operator to convert virtual circular-queue indexes into safe array positions.
How it connects
Connects modular arithmetic to circular queues, arrays, indexes, and front and rear pointers.

Also explained in

Compare how 3 books explain modulo arithmetic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.