File: sum-of-digits-in-the-minimum-number/solution.py

Date: 2026-06-06

Time: 19:21

Purpose

This file solves LeetCode 1085 — Sum of Digits in the Minimum Number. It owns a single responsibility: given an array of positive integers, find the minimum element, sum its digits, and return 1 if that sum is even, 0 if odd.

Key Components

Solution.sumofdigits(self, nums: List[int]) -> int

The only method. Its contract:

Patterns

Digit extraction via modular arithmetic. Rather than converting to a string and summing character values (sum(int(d) for d in str(min_val))), the solution uses the classic % 10 / // 10 loop. This avoids string allocation and is a common idiom in LeetCode number-manipulation problems.

Destructive iteration on a copy. min_val is consumed by the while loop — its value is zero when the loop exits. This is fine because the original list isn't mutated; min() returned a value, not a reference to the list element.

Dependencies

Flow

1. min(nums) scans the list in O(n) to find the smallest element.

2. The while loop extracts digits from right to left, accumulating their sum. It terminates when min_val reaches 0 (integer division of a single digit by 10).

3. The ternary at the return checks parity of the digit sum.

Invariants

Error Handling

None. The function trusts its caller to provide valid input per LeetCode conventions. An empty list would propagate ValueError from min().

Topics to Explore

Beliefs