[SOLUTION] – Find Numbers with Even Number of Digits – LeetCode

0
91
Find numbers with even number of digits leetcode solution

In this article, you will get the solution of the LeetCode problem :

Find Numbers with Even Number of Digits

Java Long Code : Find Numbers with Even Number of Digits

public class FindEvenNumber {

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

   public static void main(String[] args) {
      int[] nums = { 12, 345, 2, 6, 7896, 20 };
      System.out.println(findNumbers(nums));
   }

   static int findNumbers(int[] nums) {
      int count = 0;
      for (int num : nums) {
         if (even(num)) {
            count++;
         }
      }
      return count;
   }

   // Function to check weather this number contains even digit or not

   static boolean even(int num) {
      int numberOfDigits = digits(num);
      if (numberOfDigits % 2 == 0) {
         return true;
      }
      return false;
   }

   // Count number of digits in a number

   static int digits(int num) {
      int count = 0;
      while (num > 0) {
         count++;
         num = num / 10;
         // num /= 10 } return count;

      }
   }
}

Java Short code : Find Numbers with Even Number of Digits

public class FindEvenNumbers {
   public static void main(String args[]) {
      int[] a = { 12, 345, 2, 6, 7896, 20 };
      System.out.println(findNumbers(a));
   }

   static int numberOfDigits(int n) {
      int count = 0;
      while (n > 0) {
         n /= 10;
         count++;
      }
      return count;
   }

   static int findNumbers(int[] a) {
      int result = 0;
      for (int i = 0; i < a.length; i++)
         if (numberOfDigits(a[i]) % 2 == 0)
            result++;
      return result;
   }

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here