Skip to main content

Find Numbers with Even Number of Digits

Problem Description​

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

Example​

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[i] <= 10^5

Solution Approach​

Intuition:​

To efficiently determine Find Numbers with Even Number of Digits

Solution Implementation​

Code In Different Languages:​

Written by @Ishitamukherjee2004
 
class Solution {
findNumbers(nums) {
let rem = 0, cnt1 = 0, cnt2 = 0;
const n = nums.length;
for(let i=0; i<n; i++){
cnt1 = 0;
let num = nums[i];
while(num>0){
rem = num%10;
cnt1++;
num=Math.floor(num/10);
}
if(cnt1%2==0) cnt2++;
}
return cnt2;
}
}




Complexity Analysis​

  • Time Complexity: O(n∗m)O(n*m)
  • Space Complexity: O(1)O(1)
  • The time complexity is O(n∗m)O(n*m) where n is the length of the input array nums and m is the maximum number of digits in an element of the array. This is because the function iterates over each element in the array (O(n*m)) and for each element, it performs a while loop that iterates up to the number of digits in the element (O(m)).
  • The space complexity is O(1)O(1) because we are not using any extra space.