Sum of Digits in the Minimum Number - Problem

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

For example, if the minimum number is 23, the sum of digits is 2 + 3 = 5, which is odd, so return 0.

Input & Output

Example 1 — Basic Case
$ Input: nums = [99,77,33,44,11]
Output: 1
💡 Note: Minimum is 11. Sum of digits: 1 + 1 = 2 (even), so return 1
Example 2 — Odd Sum
$ Input: nums = [18,38,58,23]
Output: 0
💡 Note: Minimum is 18. Sum of digits: 1 + 8 = 9 (odd), so return 0
Example 3 — Single Digit
$ Input: nums = [7,4,9,6,2]
Output: 1
💡 Note: Minimum is 2. Sum of digits: 2 (even), so return 1

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 99

Visualization

Tap to expand
Sum of Digits in the Minimum Number INPUT nums = [99, 77, 33, 44, 11] 99 i=0 77 i=1 33 i=2 44 i=3 11 i=4 Minimum value: 11 MIN Input Details Array length: 5 Values: 99, 77, 33, 44, 11 Task: Find min, sum digits Return 0 if odd, 1 if even ALGORITHM STEPS 1 Find Minimum Scan array: min = 11 2 Extract Digits 11 has digits: 1, 1 3 Sum Digits 1 + 1 = 2 4 Check Parity 2 % 2 == 0 (even) Calculation Breakdown min(99,77,33,44,11) = 11 digitSum(11) = 1 + 1 = 2 2 is EVEN --> return 1 FINAL RESULT Output 1 Why 1? Min value: 11 Digit sum: 1+1 = 2 2 is even Even --> return 1 OK - Verified! return 1; Key Insight: Single Pass Optimization: Find minimum while iterating once through the array (O(n)). Digit sum uses modulo (% 10) and integer division (/ 10) repeatedly until number becomes 0. Final check: If sum is even (sum % 2 == 0) return 1, else return 0. Time: O(n), Space: O(1). TutorialsPoint - Sum of Digits in the Minimum Number | Optimized Single Pass Approach
Asked in
Amazon 15 Google 8
12.5K Views
Low Frequency
~5 min Avg. Time
421 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