Count Odd Even | Practice | GeeksforGeeks

Given an array A[] of N elements. The task is to count number of even and odd elements in the array.
Example:
Input:
N = 5
A[] = 1 2 3 4 5
Output:
3 2
Explanation:
There are 3 odd elements (1, 3, 5)
and 2 even elements (2 and 4).
Your 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.
Print a newline at the end of output.
Constraints:
1 <= N <= 106
1 <= Ai <= 106
0
chitranshugupta20032 days ago
void countOddEven(int arr[], int sizeof_array)   {              // your code here       int i,counti=0,counto=0;       for(i=0;i<sizeof_array;i++)       {           if(arr[i]%2!=0)           {              counti++;            }           else           {              counto++;            }       }       cout<<counti<<" "<<counto<<endl;   }   
0
atif836146 days ago
JAVA SOLUTION
 if(n==0){           return;       }       int even=0;       int odd=0;       for(int i=0;i<n;i++){           if(arr[i]%2==0){               even++;           }           else{           odd++;           }       }       System.out.print(odd+" "+even);      System.out.println();
0
mehtay0376 days ago
Python Solution:
class Solution:def countOddEven(self, arr, n): #Code here count_e,count_o=0,0 for i in arr:     if(i%2==0):         count_e+=1     else:         count_o+=1 print(count_o,count_e)         
 
0
harshscode1 week ago
  long long int even=0,odd=0;            for(int i=0;i<n;i++)      {          if(a[i]%2==0)          even=even+1;          else          odd=odd+1;      }            cout<<odd<<" "<<even<<endl;      
0
diagovenk1 week ago
Hello guys. Easy java solution using bitwise operator.
public void countOddEven(int[] arr, int n)   {       // Code here       int odd = 0;       int even = 0;       for(int i=0; i<n; i++){           if((arr[i]&1)>0){               odd++;           }           else{               even++;           }       }       System.out.print(odd+" ");       System.out.print(even);       System.out.println();   }
0
rajsinghrathour03 weeks ago
simple and easy solution in c++
 
 
 int odd=0;       int even=0;       for(int i=0;i<sizeof_array;i++){            if(arr[i]%2==1)            odd=odd+1;            else            even=even+1;                  }       cout<<odd<<" "<<even<<endl;      
0
rafysiddiky3 weeks ago
void countOddEven(int arr[], int sizeof_array)   {              int odd_count = 0 , even_count = 0;              for(int i = 0; i < sizeof_array; i++){           if(arr[i] & 1){               odd_count++;           }else{               even_count++;           }       }       cout << odd_count << " " << even_count << "\n";                }
0
ashutosh0366tripathi3 weeks ago
def countOddEven(self, arr, n):    even=0    odd=0    for i in arr:        if i%2==0:            even+=1        else:            odd+=1       print(odd,even)
0
hs00000ynr4 weeks ago
int even=0, odd=0;             for(int i=0; i<sizeof_array; i++)      {          if(arr[i]%2==0){          even++;          }                    else{          odd++;          }      }      cout<<odd<<" "<<even;   }
 
 
 
why my code is not pass the test cases
 
0
musheerjamadar10241 month ago
        int even =0;       int odd =0;              for(int a:arr){           if(a%2 == 0)           even++;           else odd++;       }       System.out.println(odd+ " "+even);
 
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.