Skip to content

1085. Sum of Digits in the Minimum Number 👎

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int sumOfDigits(vector<int>& nums) {
    int mn = ranges::min(nums);
    int sum = 0;

    while (mn > 0) {
      sum += mn % 10;
      mn /= 10;
    }

    return sum & 1 ^ 1;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public int sumOfDigits(int[] nums) {
    int mn = Arrays.stream(nums).min().getAsInt();
    int sum = 0;

    while (mn > 0) {
      sum += mn % 10;
      mn /= 10;
    }

    return sum & 1 ^ 1;
  }
}
1
2
3
class Solution:
  def sumOfDigits(self, nums: list[int]) -> int:
    return sum(int(d) for d in str(min(nums))) & 1 ^ 1