aboutsummaryrefslogtreecommitdiff
path: root/find_num_even_digit.c
diff options
context:
space:
mode:
Diffstat (limited to 'find_num_even_digit.c')
-rw-r--r--find_num_even_digit.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/find_num_even_digit.c b/find_num_even_digit.c
new file mode 100644
index 0000000..351896f
--- /dev/null
+++ b/find_num_even_digit.c
@@ -0,0 +1,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
+}