Sum of Digits in the Minimum Number - Problem

You're given an integer array nums containing positive integers. Your task is to find the minimum value in the array, calculate the sum of its digits, and then determine if this sum is odd or even.

Return:

  • 0 if the sum of digits is odd
  • 1 if the sum of digits is even

Example: If nums = [12, 34, 7, 18], the minimum is 7. The sum of digits of 7 is just 7 (odd), so return 0.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [29, 87, 14, 56]
โ€บ Output: 0
๐Ÿ’ก Note: The minimum value is 14. Sum of digits: 1 + 4 = 5, which is odd, so return 0.
example_2.py โ€” Even Sum
$ Input: nums = [99, 77, 33, 66, 55]
โ€บ Output: 1
๐Ÿ’ก Note: The minimum value is 33. Sum of digits: 3 + 3 = 6, which is even, so return 1.
example_3.py โ€” Single Digit
$ Input: nums = [7]
โ€บ Output: 0
๐Ÿ’ก Note: The minimum (and only) value is 7. Sum of digits is just 7, which is odd, so return 0.

Visualization

Tap to expand
๐Ÿ† Finding the Smallest Trophy Number29Trophy 187Trophy 214MINIMUM!56Trophy 4Analyzing Trophy #141Tens digit+4Units digit=5Sum (ODD)Result: 0Sum is odd โ†’ return 0
Understanding the Visualization
1
Survey All Trophies
Walk through all trophies once to find the one with the smallest number
2
Analyze the Winner
Take the trophy with the minimum number and examine its digits
3
Count the Digits
Add up all the individual digits on the trophy
4
Determine Odd/Even
Check if the sum is odd (return 0) or even (return 1)
Key Takeaway
๐ŸŽฏ Key Insight: We only need one pass through the array to find the minimum, then use simple math (% and /) to extract digits efficiently without string conversion.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n + d)

O(n) to find minimum plus O(d) to sum d digits of the minimum number

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 104
  • All integers are positive
Asked in
Amazon 15 Microsoft 8 Google 5 Apple 3
28.5K Views
Medium Frequency
~8 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen