Header Ads Widget

Count Odd Even Difference | Tech Mahindra coding questions

Count Odd Even Difference 

Given an array , write a program to Count Odd Even Difference . we have to find the difference of count of odd and count of even .

Note : You are expected to write code in the countOddEvenDifference function only which will receive the first parameter as the number of items in the array and second parameter as the array itself. you are not required to take input from the console.

Sample Input
input 1 : 8
input 2 : 10 20 30 40 55 66 77 83

Output
-2

Explanation:-    count of odd number are 3 { 55 ,77,83 } and count of even numbers are 5 { 10,20,30,40,66 } . So the difference is ( 3-5 =  -2 ) . 



Count Odd Even Difference 

  The objective of the code is to find the difference between count of odd numbers and count of even numbers . 

C++ Code:-
#include<bits/stdc++.h>
using namespace std;

int countOddEvenDifference(int n, int a[])
{
int count_odd=0, count_even=0;
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
count_even+=1;
}
else
count_odd+=1;
}
return (count_odd-count_even);
}

// Driver function
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<countOddEvenDifference(n,a);
return 0;
}


Output:-
8
10 20 30 40 55 77 60 83
-2

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-