1. Convert to String

class Solution:
    def sumOfDigits(self, nums: List[int]) -> int:
        mn = min(nums)
        total = 0
        for c in str(mn):
            total += int(c)
        return 1 if total % 2 == 0 else 0

Time & Space Complexity

  • Time complexity: O(n+log⁡m)O(n + \log m)
  • Space complexity: O(log⁡m)O(\log m)

Where nn is the length of the array numsnums and mm is the minimum value in numsnums.


2. Math

class Solution:
    def sumOfDigits(self, nums: List[int]) -> int:
        mn = min(nums)
        total = 0
        while mn > 0:
            total += mn % 10
            mn //= 10
        return 1 if total % 2 == 0 else 0

Time & Space Complexity

  • Time complexity: O(n+log⁡m)O(n + \log m)
  • Space complexity: O(1)O(1) extra space.

Where nn is the length of the array numsnums and mm is the minimum value in numsnums.