Count number of ordered pairs with Even and Odd Sums
Last Updated :
07 Sep, 2022
Given an array of n positive numbers, the task is to count number of ordered pairs with even and odd sum.
Examples:
Input: arr[] = {1, 2, 4}
Output: Even sum Pairs = 2, Odd sum Pairs = 4
The ordered pairs are (1, 2), (1, 4), (2, 1), (4, 1), (2, 4), (4, 2)
Pairs with Even sum: (2, 4), (4, 2)
Pairs with Odd sum: (1, 2), (1, 4), (2, 1), (4, 1)
Input: arr[] = {2, 4, 5, 9, 1, 8}
Output: Even sum Pairs = 12, Odd sum Pairs = 18
Approach:
The sum of two numbers is odd if one number is odd and another one is even. So now we have to count the even and odd numbers. As in order pair (a, b) and (b, a) both treated as different pair, therefore
Number of odd sum pairs = (count of even numbers) * (count of odd numbers) * 2
This is because every even number can pair with every odd number, and every odd number can pair with every even number. Thus multiply 2 is done in the answer.
And the number of even sum pairs will be an inversion of the number of odd sum pairs. Therefore:
Number of even sum pairs = Total Number of pairs – Number of odd sum pairs
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count_odd_pair( int n, int a[])
{
int odd = 0, even = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 2 == 0)
even++;
else
odd++;
}
int ans = odd * even * 2;
return ans;
}
int count_even_pair( int odd_sum_pairs, int n)
{
int total_pairs = (n * (n - 1));
int ans = total_pairs - odd_sum_pairs;
return ans;
}
int main()
{
int n = 6;
int a[] = { 2, 4, 5, 9, 1, 8 };
int odd_sum_pairs = count_odd_pair(n, a);
int even_sum_pairs = count_even_pair(
odd_sum_pairs, n);
cout << "Even Sum Pairs = "
<< even_sum_pairs
<< endl;
cout << "Odd Sum Pairs= "
<< odd_sum_pairs
<< endl;
return 0;
}
|
Java
class GFG
{
static int count_odd_pair( int n, int a[])
{
int odd = 0 , even = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] % 2 == 0 )
even++;
else
odd++;
}
int ans = odd * even * 2 ;
return ans;
}
static int count_even_pair( int odd_sum_pairs, int n)
{
int total_pairs = (n * (n - 1 ));
int ans = total_pairs - odd_sum_pairs;
return ans;
}
public static void main(String []args)
{
int n = 6 ;
int []a = { 2 , 4 , 5 , 9 , 1 , 8 };
int odd_sum_pairs = count_odd_pair(n, a);
int even_sum_pairs = count_even_pair( odd_sum_pairs, n);
System.out.println( "Even Sum Pairs = " + even_sum_pairs);
System.out.println( "Odd Sum Pairs= " + odd_sum_pairs);
}
}
|
Python3
def count_odd_pair( n, a):
odd = 0
even = 0
for i in range ( 0 ,n):
if ( a[ i] % 2 = = 0 ):
even = even + 1
else :
odd = odd + 1
ans = odd * even * 2
return ans
def count_even_pair( odd_sum_pairs, n):
total_pairs = ( n * ( n - 1 ))
ans = total_pairs - odd_sum_pairs
return ans
n = 6
a = [ 2 , 4 , 5 , 9 , 1 , 8 ]
odd_sum_pairs = count_odd_pair( n, a)
even_sum_pairs = count_even_pair( odd_sum_pairs, n)
print ( "Even Sum Pairs =" , even_sum_pairs)
print ( "Odd Sum Pairs=" , odd_sum_pairs)
|
C#
using System;
class GFG
{
static int count_odd_pair( int n, int []a)
{
int odd = 0, even = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 2 == 0)
even++;
else
odd++;
}
int ans = odd * even * 2;
return ans;
}
static int count_even_pair( int odd_sum_pairs, int n)
{
int total_pairs = (n * (n - 1));
int ans = total_pairs - odd_sum_pairs;
return ans;
}
public static void Main()
{
int n = 6;
int []a = { 2, 4, 5, 9, 1, 8 };
int odd_sum_pairs = count_odd_pair(n, a);
int even_sum_pairs = count_even_pair( odd_sum_pairs, n);
Console.WriteLine( "Even Sum Pairs = " + even_sum_pairs);
Console.WriteLine( "Odd Sum Pairs= " + odd_sum_pairs);
}
}
|
PHP
<?php
function count_odd_pair( $n , $a )
{
$odd = 0;
$even = 0;
for ( $i = 0; $i < $n ; $i ++) {
if ( $a [ $i ] % 2 == 0)
$even ++;
else
$odd ++;
}
$ans = $odd * $even * 2;
return $ans ;
}
function count_even_pair( $odd_sum_pairs , $n )
{
$total_pairs = ( $n * ( $n - 1));
$ans = $total_pairs - $odd_sum_pairs ;
return $ans ;
}
$n = 6;
$a = array ( 2, 4, 5, 9, 1, 8 );
$odd_sum_pairs = count_odd_pair( $n , $a );
$even_sum_pairs = count_even_pair( $odd_sum_pairs , $n );
echo "Even Sum Pairs = $even_sum_pairs \n" ;
echo "Odd Sum Pairs= $odd_sum_pairs \n" ;
?>
|
Javascript
<script>
function count_odd_pair(n, a)
{
var odd = 0, even = 0;
for ( var i = 0; i < n; i++) {
if (a[i] % 2 == 0)
even++;
else
odd++;
}
var ans = odd * even * 2;
return ans;
}
function count_even_pair(odd_sum_pairs, n)
{
var total_pairs = (n * (n - 1));
var ans = total_pairs - odd_sum_pairs;
return ans;
}
var n = 6;
var a = [2, 4, 5, 9, 1, 8];
var odd_sum_pairs = count_odd_pair(n, a);
var even_sum_pairs = count_even_pair(
odd_sum_pairs, n);
document.write( "Even Sum Pairs = "
+ even_sum_pairs + "<br>" );
document.write( "Odd Sum Pairs = "
+ odd_sum_pairs + "<br>" );
</script>
|
Output
Even Sum Pairs = 12
Odd Sum Pairs= 18
Complexity Analysis:
- Time Complexity: O(n), to find the number of odd and even numbers in the given array
- Auxiliary Space: O(1), as no extra space is used
Share your thoughts in the comments
Please Login to comment...