Find Numbers with Even Number of Digits

Updated on 27 May, 2025
Find Numbers with Even Number of Digits header image

Problem Statement

The task is to determine how many integers in a given array have an even number of digits. We are provided with an array called nums, which contains multiple integers. The objective is to count and return the number of integers in this array that have an even digit count.

Examples

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

Approach and Intuition

To solve this problem, the following approach can be adopted:

  1. Traverse through each integer in the provided nums array.
  2. For each integer, determine the total number of digits it contains.
    • One way to find the number of digits is to convert the integer to a string and then obtain the length of the string.
  3. Check if the count of digits is even.
  4. Keep a running total of integers that meet the criterion of having an even digit count.
  5. Return the final count after iterating through all integers in the array.

The constrained size of the array (up to 500 integers) ensures that a simple loop through the array will be efficient. Additionally, since Python handles large integers natively, converting integers to strings and counting their length will not cause performance issues for the maximum integer size constraint given.

Solutions

  • C++
  • Java
  • C
  • JavaScript
  • Python
cpp
class Solution {
public:
    int countEvenDigitNumbers(vector<int>& numbers) {
        int count = 0;

        for (int value : numbers) {
            if ((value >= 10 && value <= 99) || (value >= 1000 && value <= 9999) || value == 100000)
                count++;
        }

        return count;
    }
};

Create a function in C++ that counts numbers within an array having an even number of digits. Implement this by following these steps:

  1. Define a function, countEvenDigitNumbers, that accepts a vector of integers.
  2. Initialize a counter, count, to zero to store the frequency of numbers with even digits.
  3. Iterate through each number in the provided vector using a range-based for loop.
  4. Within the loop, check if a number fits the criteria of having an even number of digits. This is true for numbers with 2, 4, or 6 digits. Use conditional statements to identify numbers with lengths of 10-99, 1000-9999, or exactly 100000.
  5. Increment the count for each number that meets the criteria.
  6. Return the value of count after the loop concludes.

This approach efficiently tallies numbers with even digit counts without converting numbers to strings, which optimizes performance and utilizes simple arithmetic checks for digit count determination.

java
class Solution {
    public int countEvenDigitNumbers(int[] array) {
        int count = 0;

        for (int value : array) {
            if ((value >= 10 && value <= 99) || (value >= 1000 && value <= 9999) || value == 100000)
                count++;
        }

        return count;
    }
}

The solution involves a Java method designed to count how many numbers within an input array have an even number of digits. The logic embedded in the method countEvenDigitNumbers examines each number in the array. By applying conditions that check if the number falls within the ranges typically having even numbers of digits (10 to 99, 1000 to 9999, or exactly 100000), the method appropriately increments a counter for every number that meets these criteria. Ensure the numbers passed into the method are integers as the method checks explicitly for integer values within specified ranges. The method returns the total count of such numbers, providing a simple and efficient solution to the problem without the need for complex string manipulation or digit counting techniques. This code is particularly useful for datasets where the limits of numbers are known and confined to particular ranges.

c
int countEvenDigits(int* array, int size) {
    int count = 0;

    for (int index = 0; index < size; index++) {
        if ((array[index] >= 10 && array[index] <= 99) || (array[index] >= 1000 && array[index] <= 9999)
        || array[index] == 100000)
            count++;
    }

    return count;
}

The C program provided solves the problem of finding numbers within an array that have an even number of digits. Here's a breakdown of how the program works:

  • Define a function countEvenDigits that takes a pointer to an integer array and its size as parameters.
  • Initialize a count variable to zero to keep track of how many numbers meet the criteria.
  • Use a loop to iterate through each element in the array.
  • Inside the loop, use a conditional statement to check if the number of digits in each number is even. This is achieved by checking if the number is either:
    • A two-digit number (between 10 to 99 inclusively),
    • A four-digit number (between 1000 to 9999 inclusively),
    • Exactly 100000 (a six-digit number).
  • If a number meets any of these conditions, increment the count.
  • After the loop completes, return the count which contains the total number of numbers with an even number of digits.

This implementation efficiently counts how many numbers in the given array contain an even number of digits using conditional statements to match specific ranges and values.

js
/**
 * @param {number[]} nums
 * @return {number}
 */
var countEvenDigits = function(nums) {
    let count = 0;

    for (let i of nums) {
        if ((i >= 10 && i <= 99) || (i >= 1000 && i <= 9999) || i == 100000)
            count++;
    }

    return count;
};

The code snippet provided in JavaScript aims to solve the problem of counting numbers within an array that have an even number of digits. Follow the outlined steps below to understand the approach used in this solution:

  1. Initialize a counter (count) to zero. This counter will be used to track the number of numbers with an even number of digits.

  2. Iterate over each number in the input array nums. Use a for-loop to access each element (i).

  3. Within the loop, check if the number has an even number of digits by evaluating certain conditions:

    • A number has two digits if it is between 10 and 99 inclusive.
    • A number has four digits if it is between 1000 and 9999 inclusive.
    • A number has six digits if it is exactly 100000.
  4. If any of the above conditions hold true for a number, increment the count by one.

  5. After completing the iteration of the entire array, return the value of count. This value represents the total count of numbers in the input array that have an even number of digits.

This solution efficiently checks for numbers within specific ranges that correspond to an even number of digits, hence ensuring optimal performance and simplicity in implementation.

python
class Solution:
    def countEvenDigitNumbers(self, numbers: List[int]) -> int:
        count_of_even_digit_numbers = 0

        for number in numbers:
            if (number >= 10 and number <= 99) or (number >= 1000 and number <= 9999)\
            or number == 100000:
                count_of_even_digit_numbers += 1

        return count_of_even_digit_numbers

This solution summary outlines a Python function designed to determine the count of numbers within a list that have an even number of digits. The function countEvenDigitNumbers specifically checks for numbers with 2, 4, or 6 digits. The criteria used to check the digit length involves explicitly examining if:

  • A number is between 10 and 99 (inclusive),
  • A number is between 1000 and 9999 (inclusive),
  • A number is exactly 100000.

For each number in the list numbers that meets one of these conditions, the counter count_of_even_digit_numbers is incremented. The function culminates by returning the count of such numbers.

Highlights of the implementation:

  • Utilizes logical operator or to check if a number falls within the specified ranges or matches the exact value.
  • Employs a simple for-loop to iterate through each number in the input list, ensuring all elements are checked.
  • Lean use of control flow with a direct return of the count variable, maximizing clarity and efficiency.

Comments

No comments yet.