Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Even Number of Digits Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Even Number of Digits | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array of integers, find the elements which have an even number of digits.

Approach 

We can just iterate over the elements in the array, and count the number of digits in each element of the array, and push the elements, which have an even number of digits in a list.

Counting the digits can be done by repeatedly dividing the number with 10.

Analysis  

  • Time Complexity : O(n log (size of largest number))
  • Space Complexity : O(n)

Implementation

C++
int getDigitCount(int n) {
   int count = 0;
   if(n == 0) {
       return 1;
   }
   while (n > 0) {
       count++;
       n /= 10;
   }
   return count;
}
vector<int> getEvenDigitNumbers(vector<int> &arr) {
   vector<int> evenDigitList;
   for (int x: arr) {
       if(getDigitCount(x) % 2 == 0) {
           evenDigitList.push_back(x);
       }
   }
   return evenDigitList;
}
Java
class Solution {
   int getDigitCount(int n) {
       int count = 0;
       if(n == 0) {
           return 1;
       }
       while (n > 0) {
           count++;
           n /= 10;
       }
       return count;
   }
   List<Integer> getEvenDigitNumbers (int[] arr) {
       List<Integer> evenDigitList = new ArrayList<Integer>();  
       for (int i = 0; i < arr.length; i++) {
           if(getDigitCount(arr[i]) % 2 == 0) {
               evenDigitList.add(arr[i]);
           }
       }
       return evenDigitList;
   }
}
Related Content
Cumulative Sum
Identical Twins
Largest Contiguous Sum
Matrix Rotation
Max Consecutive Ones
Next Greater Permutation
Pascal's Triangle
Positive Cumulative Sum
Primes upto N
Row Column Zero
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet