Similar Problems

Similar Problems not available

Find Numbers With Even Number Of Digits - Leetcode Solution

Companies:

  • adobe

LeetCode:  Find Numbers With Even Number Of Digits Leetcode Solution

Difficulty: Easy

Topics: array  

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

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 Approach To solve this problem, we need to check the number of digits in each number in the input array. If there are even number of digits, we can store that number in a counter variable and return it in the end. To check the number of digits in each number, we can convert each number to its string representation and then take the length of that string. If the length of string is even, we will increase our counter.

Detailed steps:

  1. Initialize a counter variable to zero.
  2. Loop through each number in the input array.
  3. Convert the number to string and take the length of the string.
  4. If the length of the string is even, increase the counter.
  5. Return the counter variable.

Implementation in Python:

def findNumbers(nums): """ :type nums: List[int] :rtype: int """ counter = 0 # step 1 for num in nums: # step 2 if len(str(num)) % 2 == 0: # step 3 counter += 1 # step 4 return counter # step 5

Time complexity: O(n), where n is the length of the input array. Space complexity: O(1), as we are only using a counter variable to store the result.

Find Numbers With Even Number Of Digits Solution Code

1