problemeasyalgorithmsleetcode-1085leetcode 1085leetcode1085

Sum of Digits in the Minimum Number

EasyUpdated: Aug 2, 2025
Practice on:

Problem

Given an integer array nums, return 0 if the sum of the digits of the minimum integer innums is odd, or1 otherwise.

Examples

Example 1:

Input: nums = [34,23,1,24,75,33,54,8]
Output: 0
Explanation: The minimal element is 1, and the sum of those digits is 1 which is odd, so the answer is 0.

Example 2:

Input: nums = [99,77,33,66,55]
Output: 1
Explanation: The minimal element is 33, and the sum of those digits is 3 + 3 = 6 which is even, so the answer is 1.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Solution

Method 1 – Direct Calculation

Intuition

Find the minimum number in the array, sum its digits, and check if the sum is odd or even. This is a direct and efficient approach since the constraints are small.

Approach

  1. Find the minimum value in the array.
  2. Calculate the sum of its digits.
  3. If the sum is odd, return 0; otherwise, return 1.

Code

C++
class Solution {
public:
    int sumOfDigits(vector<int>& nums) {
        int mn = *min_element(nums.begin(), nums.end());
        int s = 0;
        while (mn) {
            s += mn % 10;
            mn /= 10;
        }
        return s % 2 == 0 ? 1 : 0;
    }
};
Go
func sumOfDigits(nums []int) int {
    mn := nums[0]
    for _, n := range nums {
        if n < mn {
            mn = n
        }
    }
    s := 0
    for mn > 0 {
        s += mn % 10
        mn /= 10
    }
    if s%2 == 0 {
        return 1
    }
    return 0
}
Java
class Solution {
    public int sumOfDigits(int[] nums) {
        int mn = Integer.MAX_VALUE;
        for (int n : nums) mn = Math.min(mn, n);
        int s = 0;
        while (mn > 0) {
            s += mn % 10;
            mn /= 10;
        }
        return s % 2 == 0 ? 1 : 0;
    }
}
Kotlin
class Solution {
    fun sumOfDigits(nums: IntArray): Int {
        var mn = nums.minOrNull() ?: 0
        var s = 0
        while (mn > 0) {
            s += mn % 10
            mn /= 10
        }
        return if (s % 2 == 0) 1 else 0
    }
}
Python
class Solution:
    def sumOfDigits(self, nums: list[int]) -> int:
        mn = min(nums)
        s = 0
        while mn > 0:
            s += mn % 10
            mn //= 10
        return 1 if s % 2 == 0 else 0
Rust
impl Solution {
    pub fn sum_of_digits(nums: Vec<i32>) -> i32 {
        let mut mn = *nums.iter().min().unwrap();
        let mut s = 0;
        while mn > 0 {
            s += mn % 10;
            mn /= 10;
        }
        if s % 2 == 0 { 1 } else { 0 }
    }
}
TypeScript
class Solution {
    sumOfDigits(nums: number[]): number {
        let mn = Math.min(...nums);
        let s = 0;
        while (mn > 0) {
            s += mn % 10;
            mn = Math.floor(mn / 10);
        }
        return s % 2 === 0 ? 1 : 0;
    }
}

Complexity

  • ⏰ Time complexity: O(n) — One pass to find the minimum, and at most 2 steps to sum its digits (since max value is 100).
  • 🧺 Space complexity: O(1) — Only a few variables are used.

Comments