Skip to main content

Sum of Digits in the Minimum Number - Solution & Explanation

EasyPremiumFree on FleetCodeArrayMath3 min readAsked at: Amazon
Practice this problem

Problem Statement

Given an integer array nums, return 0 if the sum of the digits of the minimum integer in nums is odd, or 1 otherwise.

 

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

Approach Overview

Problem Overview: You are given an integer array. Find the smallest element, compute the sum of its digits, and return 1 if the digit sum is even, otherwise return 0. The challenge is straightforward: identify the minimum efficiently and apply simple digit math.

Approach 1: Sort Then Process (O(n log n) time, O(1) extra space)

Sort the array so the smallest element appears at the first index. After sorting, take nums[0], repeatedly extract digits using % 10 and divide by 10 to compute the digit sum. Finally, check if the sum is even using sum % 2. This approach is easy to reason about but sorting performs unnecessary work since the full ordering of the array is not required.

Approach 2: Single Pass Minimum Scan (O(n) time, O(1) space)

Iterate through the array once and track the minimum value using a variable like minVal. This avoids sorting and directly solves the first part of the problem in linear time. After finding the minimum, compute the digit sum using repeated modulus and division operations. The final step checks parity: if the sum is even return 1, otherwise return 0. The logic relies only on basic iteration and arithmetic from Array traversal and Math digit manipulation.

The key insight is that only the minimum value matters. Any algorithm that processes the entire ordering of the array is doing extra work. A simple linear scan gives the optimal result while keeping memory usage constant.

Recommended for interviews: The single-pass scan is the expected solution. Interviewers want to see that you avoid sorting when only the minimum is required. Mentioning the sort-based idea first shows baseline reasoning, but implementing the O(n) scan demonstrates stronger algorithmic judgment and familiarity with array traversal and basic math operations.

Solution

Code

Python

Java

C++

Go

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sort then compute digit sumO(n log n)O(1)Simple implementation when sorting is already required elsewhere
Single pass minimum scan + digit sumO(n)O(1)Optimal solution when only the minimum element is needed

Video Solution

LeetCode 1085: Sum of Digits in the Minimum Number - Interview Prep Ep 8 • Fisher Coder • 767 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Sum of Digits in the Minimum Number easy or hard?
The problem is classified as Easy. It focuses on fundamental skills such as array iteration, tracking a minimum value, and performing basic arithmetic on digits. Most candidates can solve it with a straightforward O(n) scan.
Sum of Digits in the Minimum Number Python/Java solution
The implementation is identical across languages: scan the array to compute the minimum value, calculate the digit sum using repeated division and modulus operations, then return 1 if the sum is even or 0 otherwise. Python, Java, C++, and Go implementations follow the same O(n) logic.
How to solve Sum of Digits in the Minimum Number in O(n)?
Iterate through the array and maintain a variable that tracks the smallest value seen so far. Once the minimum is found, repeatedly extract digits using modulus (num % 10) and divide by 10 to accumulate the digit sum. If the sum is even return 1, otherwise return 0. This avoids sorting and keeps the runtime linear.
What is the best approach for Sum of Digits in the Minimum Number?
The best approach is a single-pass scan of the array to find the minimum element, followed by computing the sum of its digits. This method runs in O(n) time and O(1) space. After calculating the digit sum, check whether it is even to decide whether to return 1 or 0.
Is Sum of Digits in the Minimum Number asked at Google/Amazon/Meta?
This problem is categorized as Easy and mainly tests basic array traversal and digit manipulation. While the exact question is less common in top-tier interviews, the underlying concepts—finding minimum values and performing digit operations—frequently appear in screening rounds.
What data structure is used in Sum of Digits in the Minimum Number?
The primary data structure is an array. The algorithm performs a linear scan to find the minimum element and then applies mathematical digit extraction on that value. No additional data structures such as hash maps or stacks are required.
What is the time complexity of Sum of Digits in the Minimum Number?
The optimal solution runs in O(n) time because it scans the array once to find the minimum value. Computing the digit sum of the minimum number takes O(d) where d is the number of digits, which is constant for typical integer ranges. Overall complexity remains O(n) with O(1) extra space.

Ready to solve this problem?

Practice Sum of Digits in the Minimum Number with our built-in code editor and test cases.

Practice on FleetCode