Find Numbers with Even Number of Digits

🏠 ⬅️ ➑️

Given an array nums of integers, return how many of them contain an even number of digits.

Example 1:

Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits). 345 contains 3 digits (odd number of digits). 2 contains 1 digit (odd number of digits). 6 contains 1 digit (odd number of digits). 7896 contains 4 digits (even number of digits). Therefore only 12 and 7896 contain an even number of digits.

Example 2:

Input: nums = [555,901,482,1771] Output: 1 Explanation: Only 1771 contains an even number of digits.

Constraints:

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 105

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    ! Solve the problem described in the task
    implicit none

    integer :: i, n
    integer, parameter :: nums(5) = [12, 345, 2, 6, 7896]
    integer, parameter :: expected_result = 2

    print '(A)', 'Testing example 1:'
    call test_example(nums, expected_result)

    print '(A)', 'Testing example 2:'
    n = size(nums)
    allocate(nums(n+1))
    nums(n+1) = 555
    call test_example(nums, 1)

contains

    subroutine test_example(nums, expected_result)
        implicit none
        integer, intent(in) :: nums(:), expected_result
        integer :: result

        result = count_even_digits(nums)
        if (result == expected_result) then
            print '(A,I0)', 'Passed! Result is ', result
        else
            print '(A,I0)', 'Failed! Expected result is ', expected_result
        end if
    end subroutine test_example

    function count_even_digits(nums) result(count)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: count, digit, i

        count = 0
        do i = 1, size(nums)
            digit = modulo(nums(i), 10)
            if (digit == 0 .or. digit == 2 .or. digit == 4 .or. &
                digit == 6 .or. digit == 8) then
                count = count + 1
            end if
        end do
    end function count_even_digits

end program main
❌ Compiled
❌ Executed
❌ Correct
module even_digits
    implicit none
    private
    public :: count_even_digits
contains
    function count_even_digits(nums) result(count)
        integer, intent(in) :: nums(:)
        integer :: count
        integer :: i, digit
        count = 0
        do i = 1, size(nums)
            digit = mod(nums(i), 10)
            if (digit == 0) then
                count = count + 1
            end if
            nums(i) = nums(i) / 10
        end do
    end function count_even_digits
end module even_digits

program main
    use even_digits
    implicit none
    integer, parameter :: nums(4) = [12, 345, 2, 6, 7896]
    integer :: count

    count = count_even_digits(nums)
    write (*, '(A, I0)') 'The number of even digits is: ', count
end program main
🌐 Data from online sources
def min_perimeter(neededApples):
    layer = 0
    apples = 0

    while apples < neededApples:
        layer += 1
        apples += 12 * layer

    return 8 * layer

To solve this problem, we will start at the center of the garden (0, 0) and expand outwards in layers. Each new layer will enclose a new set of trees, and the number of apples in each tree can be calculated using the formula |i| + |j|. We keep track of the total apples acquired and continue expanding outwards until we have at least the number of apples needed (neededApples).

Since there are 4 corners in the square corresponding to each axis-aligned square plot, and each layer adds 2 trees per corner, we have a total of 8 trees added per layer. Thus, the perimeter added when expanding each layer is 8 units.

In each layer, we have 12 * layer of apples (this comes from calculating the number of apples added in each side of the square when expanding outwards, which forms a pattern).

The algorithm works as follows: 1. Initialize layer to 0 and apples to 0 2. While apples is less than neededApples, increment layer and add 12 * layer apples to the current count. 3. Return 8 * layer as the perimeter, as each layer increases the perimeter by 8 units.

This solution has a linear time complexity with respect to the number of apples needed.

🌐 Data from online sources
int min_perimeter(int neededApples) {
    int layer = 0;
    int apples = 0;

    while (apples < neededApples) {
        layer++;
        apples += 12 * layer;
    }

    return 8 * layer;
}

To solve this problem, we will start at the center of the garden (0, 0) and expand outwards in layers. Each new layer will enclose a new set of trees, and the number of apples in each tree can be calculated using the formula |i| + |j|. We keep track of the total apples acquired and continue expanding outwards until we have at least the number of apples needed (neededApples).

Since there are 4 corners in the square corresponding to each axis-aligned square plot, and each layer adds 2 trees per corner, we have a total of 8 trees added per layer. Thus, the perimeter added when expanding each layer is 8 units.

In each layer, we have 12 * layer of apples (this comes from calculating the number of apples added in each side of the square when expanding outwards, which forms a pattern).

The algorithm works as follows: 1. Initialize layer to 0 and apples to 0 2. While apples is less than neededApples, increment layer and add 12 * layer apples to the current count. 3. Return 8 * layer as the perimeter, as each layer increases the perimeter by 8 units.

This solution has a linear time complexity with respect to the number of apples needed.