Count Integers With Even Digit Sum

๐Ÿ  โฌ…๏ธ โžก๏ธ

Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even.

The digit sum of a positive integer is the sum of all its digits.

Example 1:

Input: num = 4 Output: 2 Explanation: The only integers less than or equal to 4 whose digit sums are even are 2 and 4.

Example 2:

Input: num = 30 Output: 14 Explanation: The 14 integers less than or equal to 30 whose digit sums are even are 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.

Constraints:

  • 1 <= num <= 1000

Note: This problem is from LeetCode.
โœ… Compiled
โŒ Executed
โŒ Correct
program main
    implicit none

    integer :: num, i, j, count
    character(len=10) :: str

    ! read input
    read(*,*) num

    ! solve problem
    count = 0
    do i = 1, num
        write(str, '(I10)') i
        j = 0
        do while (j < len_trim(str))
            j = j + 1
            if (mod(ichar(str(j:j)), 2) == 0) then
                count = count + 1
            end if
        end do
    end do

    ! print output
    write(*,'(I0)') count

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
```
program solve_problem
    implicit none
    integer :: num, i, j, digit_sum
    integer, dimension(1000) :: numbers

    read *, num

    ! Initialize the array with all numbers up to num
    do i = 1, num
        numbers(i) = i
    end do

    ! Loop through the array and remove all numbers with odd digit sums
    do i = 1, num
        digit_sum = 0
        do j = 1, i
            digit_sum = digit_sum + mod(i, 10)
        end do
        if (mod(digit_sum, 2) == 1) then
            numbers(i) = 0
        end if
    end do

    ! Count the number of remaining numbers
    count = 0
    do i = 1, num
        if (numbers(i) /= 0) then
            count = count + 1
        end if
    end do

    ! Print the result
    write (*,*) count

end program solve_problem
```
๐ŸŒ Data from online sources
def max_tasks(tasks, workers, pills, strength):
    tasks.sort()
    workers.sort()
    task_index = 0
    pill_index = pills
    completed_tasks = 0

    for worker_strength in workers:
        while pill_index > 0 and worker_strength + strength >= tasks[task_index]:
            worker_strength += strength
            pill_index -= 1

        if worker_strength >= tasks[task_index]:
            task_index += 1
            completed_tasks += 1

        if task_index >= len(tasks):
            break

    return completed_tasks
1. First, we sort both the tasks and the workers arrays in increasing order.
  1. We initialize variables: taskIndex for iterating through the tasks array, pillIndex to keep track of the available pills, and completedTasks to count the number of tasks completed.
  2. Next, we iterate through the workers array, each time checking if the current worker's strength is sufficient to complete the current task by comparing it with the task's strength requirement.
  3. If the worker's strength is not sufficient and we still have pills available, we increment the worker's strength by "strength" (the strength added by one pill) and decrement the number of available pills.
  4. If the worker's strength is now sufficient to complete the task, we increment the task index and the completed tasks counter.
  5. We stop the loop if all tasks have been completed.
  6. Finally, we return the completedTasks counter.
๐ŸŒ Data from online sources
#include <vector>
#include <algorithm>

int maxTasks(std::vector<int>& tasks, std::vector<int>& workers, int pills, int strength) {
    std::sort(tasks.begin(), tasks.end());
    std::sort(workers.begin(), workers.end());
    int taskIndex = 0;
    int pillIndex = pills;
    int completedTasks = 0;

    for (int workerStrength : workers) {
        while (pillIndex > 0 && workerStrength + strength >= tasks[taskIndex]) {
            workerStrength += strength;
            --pillIndex;
        }

        if (workerStrength >= tasks[taskIndex]) {
            ++taskIndex;
            ++completedTasks;
        }

        if (taskIndex >= tasks.size()) {
            break;
        }
    }

    return completedTasks;
}
1. First, we sort both the tasks and the workers arrays in increasing order.
  1. We initialize variables: taskIndex for iterating through the tasks array, pillIndex to keep track of the available pills, and completedTasks to count the number of tasks completed.
  2. Next, we iterate through the workers array, each time checking if the current worker's strength is sufficient to complete the current task by comparing it with the task's strength requirement.
  3. If the worker's strength is not sufficient and we still have pills available, we increment the worker's strength by "strength" (the strength added by one pill) and decrement the number of available pills.
  4. If the worker's strength is now sufficient to complete the task, we increment the task index and the completed tasks counter.
  5. We stop the loop if all tasks have been completed.
  6. Finally, we return the completedTasks counter.