Leetcode 1295: Find Numbers with Even Number of Digits

Category : Easy


Given an array nums of integers, return how many of them contain an even number of digits.



Example 1:

Example 2:


  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 10^5

Solution Approach

The problem states that we need to find a count of all the numbers in the array which has even number of digits. There are many ways to find the number of digits in a number. You can use the technique which I have showed in palindrome number which can be accessed here. I am going to introduce a new technique which will be much more efficient if we only want to know the number of digits in a number without wanting to know what those digits are.

We can make use of strings to convert a number into a string so that the length calculation becomes easier using the length() method of Java. 

To perform our calculation we need to initialise a count variable to keep the count of all the numbers with even number of digits. We loop through the array next and one by one we convert the integer to string using toString() method and calculate the length. If the length is divisible by 2 then we increase the value in the count variable. At the end of the loop, we just return the count variable’s value as our answer.

Solution code

For more Leetcode explained solutions visit Leetcode Solutions. If you like capture the flag challenges visit here.

Check out my socials below in the footer. Feel free to ask any doubts in the comment section or contact me via Contact page I will surely respond. Happy Leetcoding 

Leave a Comment

Your email address will not be published. Required fields are marked *