We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Sum of Digits in the Minimum Number

Number: 1082

Difficulty: Easy

Paid? Yes

Companies: Amazon


Problem Description

Given an integer array nums, find the minimum element, calculate the sum of its digits, and return 0 if the sum is odd, or 1 if it is even.


Key Insights

  • Identify the minimum element in the array.
  • Convert the minimum element to its digits to compute the sum.
  • Use the parity (odd/even) of the digit sum to decide the return value.
  • The array length and element range are small, so a straightforward approach works efficiently.

Space and Time Complexity

Time Complexity: O(n + d) where n is the number of elements and d is the number of digits in the minimum element (d is constant in this problem). Space Complexity: O(1) as extra space usage is minimal.


Solution

The solution involves two main steps:

  1. Iterate through the array to find the minimum element.
  2. Compute the sum of digits by converting the number to a string (or using arithmetic operations) and summing each digit. Finally, check if the digit sum is odd or even. Return 0 if odd, and 1 if even. The approach leverages basic array traversal and digit manipulation which are both simple and efficient for the given constraints.

Code Solutions

# Function to compute the result as per the problem statement
def sum_of_digits_in_minimum_number(nums):
    # Find the minimum element in the list
    minimum = min(nums)
    
    # Calculate the sum of the digits of the minimum element
    digit_sum = sum(int(d) for d in str(minimum))
    
    # If the digit sum is odd, return 0; otherwise, return 1.
    return 0 if digit_sum % 2 != 0 else 1

# Example usage:
nums = [34,23,1,24,75,33,54,8]
print(sum_of_digits_in_minimum_number(nums))  # Output: 0
← Back to All Questions