aboutsummaryrefslogtreecommitdiff
path: root/find_num_even_digit.c
blob: 351896f02a18c721bdda88e29e905809596817c6 (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
27
// 1295. Find Numbers with Even Number of Digits
#include "leetcode.h"

/*
 * given an array 'nums' of integers, return how many of them contain an even
 * number of digits.
 */

int findNumbers(int *nums, int numsSize) {
  int cnt = 0;
  for (int i = 0; i < numsSize; i++) {
    int curr = 0;
    while (nums[i]) {
      curr++;
      nums[i] /= 10;
    }
    if (!(curr & 1))
      cnt++;
  }
  return cnt;
}

int main() {
  int n1[] = {12, 345, 2, 6, 7896}, n2[] = {555, 901, 482, 1771};
  printf("%d\n", findNumbers(n1, ARRAY_SIZE(n1))); // expect: 2
  printf("%d\n", findNumbers(n2, ARRAY_SIZE(n2))); // expect: 1
}