TheAlgorithms/Python · error · ValueError
Either arr or size must be specified
Error message
Either arr or size must be specified
What it means
Raised by FenwickTree.__init__ when neither arr nor size is given. The constructor supports two modes — initialize from an array (self.init(arr)) or allocate an all-zero tree of a given size — and `FenwickTree()` with both arguments None falls into the else branch and raises ValueError. It mirrors the API of competitive-programming Fenwick implementations where an explicit extent is required.
Source
Thrown at data_structures/binary_tree/fenwick_tree.py:26
More info: https://en.wikipedia.org/wiki/Fenwick_tree
"""
def __init__(self, arr: list[int] | None = None, size: int | None = None) -> None:
"""
Constructor for the Fenwick tree
Parameters:
arr (list): list of elements to initialize the tree with (optional)
size (int): size of the Fenwick tree (if arr is None)
"""
if arr is None and size is not None:
self.size = size
self.tree = [0] * size
elif arr is not None:
self.init(arr)
else:
raise ValueError("Either arr or size must be specified")
def init(self, arr: list[int]) -> None:
"""
Initialize the Fenwick tree with arr in O(N)
Parameters:
arr (list): list of elements to initialize the tree with
Returns:
None
>>> a = [1, 2, 3, 4, 5]
>>> f1 = FenwickTree(a)
>>> f2 = FenwickTree(size=len(a))
>>> for index, value in enumerate(a):
... f2.add(index, value)
>>> f1.tree == f2.tree
TrueView on GitHub (pinned to f5988cc097)
Solutions
- Pass a size when you plan to add points incrementally: FenwickTree(size=n)
- Pass the full array when values are known up front: FenwickTree(arr=values)
- In wrappers, give one parameter a real default (e.g. size: int = 10**5) instead of None so both-None cannot reach the constructor
Example fix
# before ft = FenwickTree() # ValueError # after ft = FenwickTree(size=n) # or ft = FenwickTree(arr=[0] * n)
Defensive patterns
Strategy: validation
Validate before calling
if arr is None and size is None:
size = 1 # or raise your own descriptive error
ft = FenwickTree(arr=arr, size=size) Try / catch
try:
ft = FenwickTree(arr, size)
except ValueError:
raise ValueError('FenwickTree needs either an array or an explicit size') from None Prevention
- Always pass size (for point-update builds) or arr (for bulk init) — never neither
- In wrappers, default one parameter to a concrete value rather than None
- Watch for renamed kwargs (arr vs values) after refactors
When it happens
Trigger: FenwickTree() with no arguments; passing arr=None, size=None explicitly (e.g. forwarding optional kwargs that ended up both None); constructing inside a wrapper whose defaults swallow both parameters.
Common situations: Wrapper/config layer that makes both parameters optional; refactoring that renamed arr to values and callers now pass nothing; interactive scripts that skip the size prompt.
Related errors
- Invalid input: {edge} is the wrong length.
- Invalid input: {edge} must have length 2.
- Incorrect input: {vertex} already exists in this graph.
- Polynomial degree must be non-negative
- The number of coefficients should be equal to the degree + 1
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/3e90cee56f8513a4.
Report an issue: GitHub.