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
program main
implicit none
integer, parameter :: n = 8
integer, dimension(n) :: nums = (/ 34, 23, 1, 24, 75, 33, 54, 8 /)
integer :: i, min_index, sum_digits, result
! find the index of the minimum element in nums
min_index = minloc(nums, dim=1)
! calculate the sum of the digits of the minimum element
sum_digits = 0
do i = 1, len_trim(nums(min_index))
sum_digits = sum_digits + mod(nums(min_index), 10)
nums(min_index) = nums(min_index) / 10
end do
! return 0 if the sum of the digits is odd, otherwise return 1
result = merge(0, 1, mod(sum_digits, 2) == 0)
print '(I0)', result
end program main
temp.f95:13:23: 13 | do i = 1, len_trim(nums(min_index)) | 1 Error: โstringโ argument of โlen_trimโ intrinsic at (1) must be CHARACTER
module min_digit_sum
implicit none
contains
function solve(nums) result(output)
integer, intent(in) :: nums(:)
integer :: output
integer :: min_num, sum_digits
! Find the minimum element in nums
min_num = minval(nums)
! Calculate the sum of the digits of the minimum element
sum_digits = 0
do while (min_num > 0)
sum_digits = sum_digits + mod(min_num, 10)
min_num = min_num / 10
end do
! Return 0 if the sum of the digits is odd, or 1 otherwise
output = 1
if (mod(sum_digits, 2) == 1) then
output = 0
end if
end function solve
end module
program main
use min_digit_sum
implicit none
integer, parameter :: nums(8) = [34, 23, 1, 24, 75, 33, 54, 8]
integer :: output
output = solve(nums)
write (*, '(I1)') output
end program
0
def sum_of_digits(nums):
min_val = min(nums)
digit_sum = sum(int(digit) for digit in str(min_val))
return 1 if digit_sum % 2 == 0 else 0
1. Find the minimal element (`minVal`) of given array `nums`.
digitSum
).int sumOfDigits(vector<int>& nums) {
int minVal = *min_element(nums.begin(), nums.end());
int digitSum = 0;
while (minVal > 0) {
digitSum += minVal % 10;
minVal /= 10;
}
return digitSum % 2 == 0 ? 1 : 0;
}
1. Find the minimal element (`minVal`) of given array `nums`.
digitSum
).