Similar Problems

Similar Problems not available

Finding 3 Digit Even Numbers - Leetcode Solution

Companies:

LeetCode:  Finding 3 Digit Even Numbers Leetcode Solution

Difficulty: Easy

Topics: hash-table sorting array  

Problem Statement:

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.

Solution:

One way to solve this problem is by iterating through each number in the array and checking if it has an even number of digits. To do this, we can convert each number to a string and count the length of the string. If the length is even, we increment a counter. At the end, we return the counter.

Implementation:

To implement this solution in Python, we can define a function findNumbers that takes an array of integers as input and returns an integer representing the count of numbers with even number of digits.

def findNumbers(nums): count = 0 for num in nums: if len(str(num)) % 2 == 0: # checking if the number has even number of digits. count += 1 return count

Time Complexity:

The time complexity of this solution is O(n), where n is the length of the input array. This is because we are iterating through each number in the array and performing a constant amount of work for each number.

Space Complexity:

The space complexity of this solution is O(1), since we are only using a constant amount of extra space to store the counter and the string representation of each number.

Finding 3 Digit Even Numbers Solution Code

1