Find the count of even odd pairs in a given Array
Given an array arr[], the task is to find the count even-odd pairs in the array.
Examples:
Input: arr[] = { 1, 2, 1, 3 }
Output: 2
Explanation:
The 2 pairs of the form (even, odd) are {2, 1} and {2, 3}.
Input: arr[] = { 5, 4, 1, 2, 3}
Output: 3
Naive Approach:
- Run two nested loops to get all the possible pairs of numbers in the array.
- For each pair, check whether if a[i] is even and a[j] is odd.
- If it is, then increase the required count by one.
- When all the pairs have been checked, print the count in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCount( int arr[], int n)
{
int res = 0;
for ( int i = 0; i < n - 1; i++)
for ( int j = i + 1; j < n; j++)
if ((arr[i] % 2 == 0)
&& (arr[j] % 2 == 1)) {
res++;
}
return res;
}
int main()
{
int a[] = { 5, 4, 1, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << findCount(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findCount( int arr[], int n)
{
int res = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
for ( int j = i + 1 ; j < n; j++)
if ((arr[i] % 2 == 0 ) &&
(arr[j] % 2 == 1 ))
{
res++;
}
return res;
}
public static void main(String[] args)
{
int a[] = { 5 , 4 , 1 , 2 , 3 };
int n = a.length;
System.out.print(findCount(a, n));
}
}
|
Python3
def findCount(arr, n):
res = 0
for i in range ( 0 , n - 1 ):
for j in range (i + 1 , n):
if ((arr[i] % 2 = = 0 ) and
(arr[j] % 2 = = 1 )):
res = res + 1
return res
a = [ 5 , 4 , 1 , 2 , 3 ]
n = len (a)
print (findCount(a, n))
|
C#
using System;
class GFG{
static int findCount( int []arr, int n)
{
int res = 0;
for ( int i = 0; i < n - 1; i++)
for ( int j = i + 1; j < n; j++)
if ((arr[i] % 2 == 0) &&
(arr[j] % 2 == 1))
{
res++;
}
return res;
}
public static void Main(String[] args)
{
int []a = { 5, 4, 1, 2, 3 };
int n = a.Length;
Console.Write(findCount(a, n));
}
}
|
Javascript
<script>
function findCount(arr, n)
{
let res = 0;
for (let i = 0; i < n - 1; i++)
for (let j = i + 1; j < n; j++)
if ((arr[i] % 2 == 0)
&& (arr[j] % 2 == 1)) {
res++;
}
return res;
}
let a = [ 5, 4, 1, 2, 3 ];
let n = a.length;
document.write(findCount(a, n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Efficient Approach:
- For each element starting from index 0 we will check if it’s even or not.
- If it’s even we will increase the count by 1.
- Else we will increase our final answer by count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCount( int arr[], int n)
{
int count = 0, ans = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
count++;
else
ans = ans + count;
}
return ans;
}
int main()
{
int a[] = { 5, 4, 1, 2, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << findCount(a, n);
return 0;
}
|
Java
class GFG{
static int findCount( int arr[], int n)
{
int count = 0 , ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 == 0 )
{
count++;
}
else
{
ans = ans + count;
}
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 5 , 4 , 1 , 2 , 3 };
int n = a.length;
System.out.print(findCount(a, n));
}
}
|
Python3
def findCount(arr, n):
count = 0
ans = 0
for i in range ( 0 , n):
if (arr[i] % 2 = = 0 ):
count = count + 1
else :
ans = ans + count
return ans
a = [ 5 , 4 , 1 , 2 , 3 ]
n = len (a)
print (findCount(a, n))
|
C#
using System;
class GFG{
static int findCount( int []arr, int n)
{
int count = 0, ans = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
{
count++;
}
else
{
ans = ans + count;
}
}
return ans;
}
public static void Main()
{
int []a = { 5, 4, 1, 2, 3 };
int n = a.Length;
Console.WriteLine(findCount(a, n));
}
}
|
Javascript
<script>
function findCount(arr, n)
{
let count = 0, ans = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
count++;
else
ans = ans + count;
}
return ans;
}
let a = [ 5, 4, 1, 2, 3 ];
let n = a.length;
document.write(findCount(a, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
08 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...