aboutsummaryrefslogtreecommitdiff
path: root/count_ints_even_sum.cpp
blob: 83070c611fc7aa06d45cdfeb60c9bf7c44347f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 2180. Count Integers With Even Digit Sum
#include "leetcode.h"

/*
 * given a positive integer 'num', return the number of positive integersless
 * than or equal to 'num' whose digit sums are event. the digitsum of a positive
 * integer is the sum of all of its digits.
 */

class Solution {
public:
  int countEven(int num) {
    int tmp = num, sum = 0;
    while (num > 0) {
      sum += num % 10;
      num /= 10;
    }
    return sum % 2 == 0 ? tmp / 2 : (tmp - 1) / 2;
  }
};

int main() {
  Solution obj;
  printf("%d\n", obj.countEven(4));  // expect: 2
  printf("%d\n", obj.countEven(30)); // expect: 14
}