Sum of Digits in the Minimum Number

Similar Problems:

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.

Note:

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

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution:

// Blog link: https://code.dennyzhang.com/sum-of-digits-in-the-minimum-number // Basic Idea: Find the smallest element, then add digits one by one // Complexity: Time O(n), Space O(1) func sumOfDigits(A []int) int { t := 100 for _, v := range A { if v < t { t = v } } // add digits d := 0 for t!=0 { d += t%10 t = t/10 } return 1 - d%2 }