loading...

Data Structures & Algorithms using Rust: Find Numbers with Even Number of Digits

ssivakumar77 profile image Sivakumar Updated on ・1 min read

As part of Data Structures and Algorithms implementation using Rust, today we will explore Find Numbers with Even Number of Digits problem.

Leetcode problem link: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

Problem Statement
Given an array nums of integers, return how many of them contain an even number of digits.
Enter fullscreen mode Exit fullscreen mode
Instructions
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.
Enter fullscreen mode Exit fullscreen mode
Solution

Here is my solution to the above mentioned problem using Rust.

impl Solution {
    pub fn find_numbers(nums: Vec<i32>) -> i32 {
        nums.iter().filter(|&x| {
            let mut i_cnt: i32 = 1;
            let mut i: i32 = *x;
            while i / 10 > 0 {
                i = i / 10;
                i_cnt += 1;
            }
            i_cnt % 2 == 0
        }).count() as i32
    }
}
Enter fullscreen mode Exit fullscreen mode

Please feel free to let me know your feedback.

Happy reading!!!

Discussion

pic
Editor guide
Collapse
codingcarter profile image
Coding Carter

You can just use the bitwise AND (&) to check if a number is odd or not. Here is an example:

console.log(1 & 1); // logs `1`
console.log(2 & 1); // logs `0`
Enter fullscreen mode Exit fullscreen mode

The reason this works is because the AND operator checks if the bits (in 2 bit) on the two things are matching. In our case, these are 10-bit numbers. Our 10-bit numbers will be converted into 2 bits. If all present bits are equal, then it returns 1, else 0. Here is a figurative expression of the code above:

// These numbers are in 2-bits
1 AND 1 // returns `1`
10 AND 1 // returns  `0`
Enter fullscreen mode Exit fullscreen mode

In the first case, both numbers have a 1 in the 1s place, so it returns 1 (true). In the second case, the first number has a 0 in the 1s place, but the second number has a 1, therefore it returns 0 (false).

Collapse
ssivakumar77 profile image
Sivakumar Author

Thanks Coding Carter for your feedback.

As per my understanding, the problem statement is to find an element in Array which is "Even" interms of number of digits. For ex: 342 is an Even number but in terms of number of digits it is not "Even".

Hope this explains.

Please feel free to share your feedback.

Collapse
codingcarter profile image
Coding Carter

Yes, but couldn't you do i_cnt & 1 instead of i_cnt % 2 == 0?

Thread Thread
ssivakumar77 profile image
Sivakumar Author

Yes, that can be refactored as you suggested.