Divisibility
Mathematics
Number Theory
Lists
Algorithms

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.

Practice algorithms

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:

  1. For each index i, B[i] divides A[i].
  2. There exists an integer list C where A[i] = B[i] * C[i].

Under this model, divisibility is checked index by index, not by list sum or product.

Example:

  1. A = [10, 20, 30]
  2. 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:

  1. If B[i] == 0 and A[i] != 0, not divisible.
  2. If B[i] == 0 and A[i] == 0, treat as compatible in many practical systems.
  3. If B[i] != 0, require A[i] % B[i] == 0.

Decide and document rule 2 explicitly, because some domains may define it differently.

Basic Python check implementation

python
1from typing import List
2
3
4def is_elementwise_divisible(a: List[int], b: List[int]) -> bool:
5    if len(a) != len(b):
6        return False
7
8    for x, y in zip(a, b):
9        if y == 0:
10            if x != 0:
11                return False
12        else:
13            if x % y != 0:
14                return False
15
16    return True
17
18
19print(is_elementwise_divisible([10, 20, 30], [2, 5, 3]))
20print(is_elementwise_divisible([15, 9, 12], [4, 3, 6]))

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.

python
1from typing import List, Optional
2
3
4def quotient_list(a: List[int], b: List[int]) -> Optional[List[int]]:
5    if len(a) != len(b):
6        return None
7
8    q: List[int] = []
9    for x, y in zip(a, b):
10        if y == 0:
11            if x != 0:
12                return None
13            q.append(0)  # convention choice for 0/0 case
14        else:
15            if x % y != 0:
16                return None
17            q.append(x // y)
18
19    return q
20
21
22print(quotient_list([10, 20, 30], [2, 5, 3]))

Returning quotient often simplifies downstream computations.

Alternative meanings you may encounter

Some teams use “list divisible” for other checks:

  1. Product divisibility, product of A divisible by product of B.
  2. Multiset divisibility over prime factors.
  3. 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:

  1. Modulo operations can dominate runtime.
  2. Early exit on first failure is important.
  3. 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:

  1. Input type enforcement.
  2. Length mismatch errors.
  3. Zero-division conventions in docs.
  4. Signed integer behavior expectations.

Clear contract prevents ambiguous bug reports later.

Property-based testing idea

For high confidence, add tests based on invariant properties:

  1. If A divisible by B and B divisible by D, then A divisible by D where defined.
  2. If quotient exists, reconstructing B[i] * Q[i] returns A[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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.