Count number of ordered pairs with Even and Odd Sums
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 other one is be 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 inversion of 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:
CPP
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // function to count odd sum pair int count_odd_pair(int n, int a[]) { int odd = 0, even = 0; for (int i = 0; i < n; i++) { // if number is even if (a[i] % 2 == 0) even++; // if number is odd else odd++; } // count of ordered pairs int ans = odd * even * 2; return ans; } // function to count even sum pair 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; } // Driver code 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
// Java implementation of the above approach class GFG { // function to count odd sum pair static int count_odd_pair(int n, int a[]) { int odd = 0, even = 0; for (int i = 0; i < n; i++) { // if number is even if (a[i] % 2 == 0) even++; // if number is odd else odd++; } // count of ordered pairs int ans = odd * even * 2; return ans; } // function to count even sum pair 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; } // Driver code 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); } } // This code is contributed by ihritik |
Python3
# Pytho3 implementation of the above approach # function to count odd sum pair def count_odd_pair( n, a): odd = 0 even = 0 for i in range(0,n): # if number is even if ( a[ i] % 2 == 0): even=even+1 # if number is odd else: odd=odd+1 # count of ordered pairs ans = odd * even * 2 return ans # function to count even sum pair def count_even_pair( odd_sum_pairs, n): total_pairs = ( n * ( n - 1)) ans = total_pairs - odd_sum_pairs return ans # Driver code 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) # This code is contributed by ihritik |
C#
// C# implementation of the above approach using System; class GFG { // function to count odd sum pair static int count_odd_pair(int n, int []a) { int odd = 0, even = 0; for (int i = 0; i < n; i++) { // if number is even if (a[i] % 2 == 0) even++; // if number is odd else odd++; } // count of ordered pairs int ans = odd * even * 2; return ans; } // function to count even sum pair 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; } // Driver code 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); } } // This code is contributed by ihritik |
PHP
<?php // PHP implementation of the above approach // function to count odd sum pair function count_odd_pair($n, $a) { $odd = 0; $even = 0; for ($i = 0; $i < $n; $i++) { // if number is even if ($a[$i] % 2 == 0) $even++; // if number is odd else $odd++; } // count of ordered pairs $ans = $odd * $even * 2; return $ans; } // function to count even sum pair function count_even_pair($odd_sum_pairs, $n) { $total_pairs = ($n * ($n - 1)); $ans = $total_pairs - $odd_sum_pairs; return $ans; } // Driver code $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"; // This code is contributed by ihritik ?> |
Even Sum Pairs = 12 Odd Sum Pairs= 18
Recommended Posts:
- Count number of ordered pairs with Even and Odd Product
- Count ordered pairs with product less than N
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count of pairs between two arrays such that the sums are distinct
- Minimum count of elements that sums to a given number
- Find k ordered pairs in array with minimum difference d
- Find k pairs with smallest sums in two arrays
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Count pairs with Bitwise XOR as EVEN number
- Given two arrays count all pairs whose sum is an odd number
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Count pairs with sum as a prime number and less than n
- Count pairs with Bitwise OR as Even number
- Count pairs with Bitwise AND as ODD number
- Count number of equal pairs in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ihritik



