Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1085. Sum of Digits in the Minimum Number - Leetcode Solution

Code Implementation

class Solution:
    def sumOfDigits(self, nums):
        min_num = min(nums)
        return sum(int(d) for d in str(abs(min_num)))
      
class Solution {
public:
    int sumOfDigits(vector<int>& nums) {
        int minNum = *min_element(nums.begin(), nums.end());
        int sum = 0;
        minNum = abs(minNum);
        while (minNum) {
            sum += minNum % 10;
            minNum /= 10;
        }
        return sum;
    }
};
      
class Solution {
    public int sumOfDigits(int[] nums) {
        int minNum = Integer.MAX_VALUE;
        for (int n : nums) {
            minNum = Math.min(minNum, Math.abs(n));
        }
        int sum = 0;
        minNum = Math.abs(minNum);
        while (minNum > 0) {
            sum += minNum % 10;
            minNum /= 10;
        }
        return sum;
    }
}
      
var sumOfDigits = function(nums) {
    let minNum = Math.min(...nums.map(Math.abs));
    let sum = 0;
    minNum = Math.abs(minNum);
    while (minNum > 0) {
        sum += minNum % 10;
        minNum = Math.floor(minNum / 10);
    }
    return sum;
};
      

Problem Description

You are given an array of integers called nums. The task is to find the minimum number in the array, and then return the sum of its digits. If the minimum number is negative, consider its absolute value when summing the digits.

  • Input: An array nums of integers (may include negatives).
  • Output: An integer representing the sum of the digits of the smallest number in nums.
  • Constraints: The array is non-empty. The minimum number is unique. If negative, use its absolute value for digit-sum.

Thought Process

To solve this problem, first, we need to identify the smallest number in the array. Once we have it, we need to compute the sum of its digits. If the number is negative, we should ignore the sign and sum the digits of its absolute value.

The brute-force approach would be to:

  • Loop through the array to find the minimum value.
  • Convert the minimum value to a string, split it into digits, and sum them up.
This is already efficient, but we can further optimize by not converting to a string and instead using mathematical operations to extract and sum digits.

The key insight is that finding a minimum in an array and summing digits are both straightforward operations, so the problem is not about complex data structures but about careful handling of negatives and digit extraction.

Solution Approach

  • Step 1: Find the Minimum Number
    • Iterate through the nums array and keep track of the smallest value found.
    • Alternatively, use built-in functions like min() for Python or Math.min() for JavaScript/Java.
  • Step 2: Take the Absolute Value
    • If the minimum number is negative, convert it to its absolute value to ignore the sign when summing digits.
  • Step 3: Sum the Digits
    • Extract each digit by repeatedly dividing by 10 and taking the remainder.
    • Accumulate the sum as you process each digit.
    • Alternatively, convert the number to a string and sum the integer value of each character (digit).
  • Step 4: Return the Result
    • Return the sum as the final answer.

This approach is efficient and easy to implement in any programming language, as shown in the code section.

Example Walkthrough

Consider the input nums = [34, 23, 1, 24, 75, 33, 54, 8].

  1. Find the minimum number in the array:
    • The smallest number is 1.
  2. Take its absolute value:
    • abs(1) = 1
  3. Sum its digits:
    • Only one digit: 1
  4. Return 1 as the result.

Let's try another input: nums = [-15, -2, -30, 7].

  1. Minimum number is -30.
  2. Absolute value is 30.
  3. Sum digits: 3 + 0 = 3.
  4. Return 3 as the result.

Time and Space Complexity

  • Brute-force Approach:
    • Finding the minimum: O(n) where n is the length of nums.
    • Summing digits: O(d) where d is the number of digits in the minimum number.
    • Total: O(n + d), but since d is much smaller than n, overall complexity is O(n).
    • Space: O(1) extra space (not counting input array).
  • Optimized Approach:
    • Same as brute-force, since both steps are already optimal for this problem.
    • No extra data structures are needed, so space remains O(1).

Thus, the solution is efficient for large arrays.

Summary

The "Sum of Digits in the Minimum Number" problem is straightforward: find the smallest number in the array, take its absolute value, and sum its digits. The main insight is to handle negatives correctly and efficiently sum digits. The solution is elegant because it leverages simple array traversal and digit extraction, resulting in clear, fast, and memory-efficient code.