Count Odd Even - Java | Practice | GeeksforGeeks

Congrats on completing Module 1.
In the previous module we learnt about the basics of Java.  Now, we'll move to more complex stuff. Here, we'll learn about arrays.
Given an array A of N elements. The task is to count number of even and odd elements in the array.
Example 1:
Input:
5
1 2 3 4 5

Output:
3 2

Explanation:
In the given array, there are 3 odd elements 
(1, 3, 5) and 2 even elements (2 and 4).
Example 2:
Input:
4
1 6 18 7

Output:
2 2

Explanation:
In the given array, there are 2 odd
elements (1, 7) and 2 even elements (6 and 18).
User Task:
Your task is to complete the function countOddEven() which should print number of odd and number of even elements in a single line seperated by space. You don't need to provide the new line.
Constraints:
1 <= N <= 106
1 <= Ai <= 106
0
sahilkumarkatiriya4 days ago
Very simple logic:
 
class Geeks {   static void countOddEven(int a[], int n) {
       // Your code here       int odd=0,even=0;      for(int i=0;i<n;i++)   {       if(a[i]%2==0)       {           even++;       }   }   odd=n-even;   System.out.print(odd + " " + even);   }}
0
mycountryakash1 month ago
class Geeks {   static void countOddEven(int a[], int n) {
       // Your code here        int Evencount =0;   int oddCount = 0;   for(int i=0;i<n;i++){       if((a[i] & 1 )==1){           oddCount =oddCount+1 ;       }else{           Evencount =Evencount+1;       }   }     System.out.print(oddCount +" " + Evencount);   }}
+1
shrutidhongade161 month ago
class Geeks {
    static void countOddEven(int a[], int n) {
        
        int even = 0;
        int odd = 0;
        for (int i=0; i< a.length; i++){
            if (a[i] % 2 == 0){
                even += 1;
            }
            else{
                odd += 1;
            }
        }
        
        System.out.printf("%d %d",odd, even);

    }
}
0
ravi119033852 months ago
// Your code here       int count_even=0;       int count_odd=0;       for(int i=0;i<n;i++)       {           if(a[i]%2==0)           count_even++;           else           count_odd++;       }       System.out.print(count_odd+" "+count_even);
0
aakasshuit2 months ago
//Java Solutio

        int odd=0,even=0;
        for(int i=0;i<n;i++){
            if(a[i]%2==0){
                even++;
            }else{
                odd++;
            }
        }
        System.out.print(odd+" "+even);
0
patelshivanshu20172 months ago
class Geeks {   static void countOddEven(int a[], int n) {
       // Your code here       int counte=0;       int countodd=0;       for(int i=0;i<n;i++){           if(a[i]%2==0){               counte++;                         }else{                          countodd++;           }       }        System.out.print(countodd+" "+counte);   }}
0
rajeevdash073 months ago
EASY JAVA SOLUTION
 
int count_even=0;           int count_odd=0;                      for(int i = 0;i < n; i++){               if(a[i]==0){                   continue;               }               else if(a[i]%2==0){                   count_even++;               }               else{                   count_odd++;               }           }                      System.out.print(count_odd+" "+count_even);
0
rajeevdash07
This comment was deleted.
0
harsh84253 months ago
class Geeks {   static void countOddEven(int a[], int n) {
       // Your code here       long even=0,odd=0;       for(int i=0;i<a.length;i++){           if(a[i] == 0){               continue;           }           else if(a[i]%2==0){               even++;           }           else{               odd++;           }                  }   System.out.print(odd+" "+even);   }}
+2
ayush90833 months ago
→ You need only one variable to print the count of both ‘odd’ and ‘even’ numbers in an array.
    => if there is x number of odd numbers in an array, then it's simply means that there is only n-x even numbers in that same array. Where n is the size of the given array.
 
→ Use bitwise AND for fast execution.
//solution
class Geeks {   static void countOddEven(int a[], int n) {       int x = 0;       for(int i : a)           if((i&1) == 1)               x++;          System.out.print(x + " " + (n-x));   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.