1085. Sum of Digits in the Minimum Number

Given an array A of positive integers, let S be the sum of the digits of the minimal element of A.

Return 0 if S is odd, otherwise return 1.

 

Example 1:

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

Example 2:

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

 

Constraints:

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

Rust Solution

struct Solution;

use std::i32;

impl Solution {
    fn sum_of_digits(a: Vec<i32>) -> i32 {
        let mut min = i32::MAX;
        for x in a {
            min = i32::min(x, min);
        }
        let mut sum = 0;
        while min > 0 {
            sum += min % 10;
            min /= 10;
        }
        if sum % 2 == 0 {
            1
        } else {
            0
        }
    }
}

#[test]
fn test() {
    let a = vec![34, 23, 1, 24, 75, 33, 54, 8];
    assert_eq!(Solution::sum_of_digits(a), 0);
    let a = vec![99, 77, 33, 66, 55];
    assert_eq!(Solution::sum_of_digits(a), 1);
}

Having problems with this solution? Click here to submit an issue on github.