Sum of Digits in the Minimum Number

easy math digit sum parity

Problem

Given an integer array nums, find the smallest value among its elements. Let S be the sum of the digits of that minimum value. Return 0 if S is odd, or 1 if S is even.

Inputnums = [34, 23, 1, 24, 75, 33, 54, 8]
Output0
The minimum is 1, whose digit sum is 1 (odd), so the answer is 0.

def sum_of_digits(nums):
    m = min(nums)
    s = 0
    while m > 0:
        s += m % 10
        m //= 10
    return 1 - s % 2
function sumOfDigits(nums) {
  let m = Math.min(...nums);
  let s = 0;
  while (m > 0) {
    s += m % 10;
    m = Math.floor(m / 10);
  }
  return 1 - (s % 2);
}
class Solution {
    public int sumOfDigits(int[] nums) {
        int m = nums[0];
        for (int x : nums) m = Math.min(m, x);
        int s = 0;
        while (m > 0) {
            s += m % 10;
            m /= 10;
        }
        return 1 - s % 2;
    }
}
int sumOfDigits(vector<int>& nums) {
    int m = *min_element(nums.begin(), nums.end());
    int s = 0;
    while (m > 0) {
        s += m % 10;
        m /= 10;
    }
    return 1 - s % 2;
}
Time: O(n) Space: O(1)