Is a list potentially divisible by another?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
“List divisibility” can mean different things, so most confusion comes from missing definitions rather than hard math. In algorithm discussions, the common interpretation is element-wise divisibility between two equal-length integer lists. This guide defines useful variants and shows efficient checks you can implement directly.
Core Topic Sections
Define the divisibility model clearly
Given two integer lists A and B of equal length, element-wise divisibility means:
- For each index
i,B[i]dividesA[i]. - There exists an integer list
CwhereA[i] = B[i] * C[i].
Under this model, divisibility is checked index by index, not by list sum or product.
Example:
A = [10, 20, 30]B = [2, 5, 3]
This is divisible because quotient list is [5, 4, 10].
Handle zero values correctly
Zero handling is usually where implementations break.
Rules that keep logic consistent:
- If
B[i] == 0andA[i] != 0, not divisible. - If
B[i] == 0andA[i] == 0, treat as compatible in many practical systems. - If
B[i] != 0, requireA[i] % B[i] == 0.
Decide and document rule 2 explicitly, because some domains may define it differently.
Basic Python check implementation
This is linear in list length and adequate for most applications.
Return quotient list when divisible
Often you need not only true or false, but also the quotient list.
Returning quotient often simplifies downstream computations.
Alternative meanings you may encounter
Some teams use “list divisible” for other checks:
- Product divisibility, product of
Adivisible by product ofB. - Multiset divisibility over prime factors.
- Polynomial or vector-space style divisibility analogies.
If you inherit existing code, inspect tests first to understand intended meaning.
Performance and large integer considerations
Element-wise checks are O(n) arithmetic operations. For huge integers:
- Modulo operations can dominate runtime.
- Early exit on first failure is important.
- Vectorized numeric libraries may help if data fits fixed-width types.
For ordinary integer sizes, the simple loop is usually fastest and easiest to maintain.
Data validation for robust APIs
When this check is exposed as API behavior, add validation around:
- Input type enforcement.
- Length mismatch errors.
- Zero-division conventions in docs.
- Signed integer behavior expectations.
Clear contract prevents ambiguous bug reports later.
Property-based testing idea
For high confidence, add tests based on invariant properties:
- If
Adivisible byBandBdivisible byD, thenAdivisible byDwhere defined. - If quotient exists, reconstructing
B[i] * Q[i]returnsA[i].
These tests catch many edge-case bugs automatically.
Common Pitfalls
- Using undefined or inconsistent rules for zero entries in divisor list.
- Mixing element-wise divisibility with product-based divisibility semantics.
- Forgetting list length checks before element operations.
- Returning integer division results without verifying exact divisibility first.
- Leaving sign behavior undocumented for negative values.
Summary
- List divisibility needs an explicit definition before implementation.
- The standard element-wise model is simple and efficient.
- Zero handling should be a deliberate documented policy.
- '
O(n)checks are sufficient for most workloads.' - Returning quotient list can make downstream logic cleaner and safer.
Related reading
- Is A really better than Dijkstra in real-world path finding?
- Is a resultant red-black tree after insertion unique?
- Is a Trie a K-ary tree?
- Is Algorithm Design Manual a good book for a beginner in algorithms?
- Is a Python dictionary an example of a hash table?
- Is a Python list guaranteed to have its elements stay in the order they are inserted in?
- is a non-decreasing sequence increasing?
- Is it better for a realtime multiplayer game server to send game statistics outbound via a socket or wait for an inbound socket connection?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.