Find Numbers with Even Number of Digits
EASYDescription
Given an array nums of integers, return how many of them contain an even number of digits.
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 <= 5001 <= nums[i] <= 105
Approaches
Checkout 4 different approaches to solve Find Numbers with Even Number of Digits. Click on different approaches to view the approach and algorithm in detail.
Brute Force using String Conversion
This approach iterates through each number in the input array. For each number, it converts it into a string and then checks the length of the string. If the length is an even number, a counter is incremented.
Algorithm
- Initialize a counter
evenDigitCountto 0. - Loop through each integer
numin thenumsarray. - Convert the integer
numto its string representation usingString.valueOf(num). - Find the length of the resulting string.
- Check if the length is divisible by 2 (i.e.,
length % 2 == 0). - If the length is even, increment
evenDigitCount. - After iterating through all the numbers, return
evenDigitCount.
This method provides a straightforward way to solve the problem by leveraging built-in string functionalities. The core idea is that the number of digits in an integer is equivalent to the length of its string representation.
- We initialize a counter
evenDigitCountto 0. - We then loop through each integer
numin the inputnumsarray. - Inside the loop, we convert the current number
numto a string. For example, the integer123becomes the string"123". - We then get the length of this string. The length of
"123"is 3. - We check if this length is an even number using the modulo operator (
length % 2 == 0). - If the condition is true, we increment our
evenDigitCount. - After the loop has processed all numbers in the array, the
evenDigitCountholds the total count of numbers with an even number of digits, which we then return.
class Solution {
public int findNumbers(int[] nums) {
int evenDigitCount = 0;
for (int num : nums) {
// Convert the number to a string
String s = String.valueOf(num);
// Check if the length of the string is even
if (s.length() % 2 == 0) {
evenDigitCount++;
}
}
return evenDigitCount;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- The logic is very direct and easy to reason about.
- Incurs the overhead of string conversion and memory allocation for each number, which is generally less efficient than purely mathematical approaches.
Code Solutions
Checking out 4 solutions in different languages for Find Numbers with Even Number of Digits. Click on different languages to view the code.
class Solution {
public
int findNumbers(int[] nums) {
int ans = 0;
for (int v : nums) {
if (String.valueOf(v).length() % 2 == 0) {
++ans;
}
}
return ans;
}
}
Video Solution
Watch the video walkthrough for Find Numbers with Even Number of Digits
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.