Count of odd and even sum pairs in an array
Last Updated :
04 Jun, 2021
Given an array arr[] of N positive integers, the task is to find the number of pairs with odd sum and the number of pairs with even sum.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output:
Odd pairs = 6
Even pairs = 4
Input: arr[] = {7, 4, 3, 2}
Output:
Odd pairs = 4
Even pairs = 2
Naive Approach:
The naive approach for this problem is to go through every pair of elements in the array, check for their sums and then count the number of pairs having odd and even sum. This approach will take O(N*N) time.
Efficient Approach:
- Count the number of odd and even elements from the array and store them in variables cntEven and cntOdd.
- In order to get the pair sum as even, all the even elements will be paired with only even elements and all the odd elements will be paired with only odd elements and the count will be ((cntEven * (cntEven - 1)) / 2) + ((cntOdd * (cntOdd - 1)) / 2)
- Now, for the sum to be odd, one of the elements of the pair must be even and the other must be odd and the required count will be cntEven * cntOdd.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
void findPairs(int arr[], int n)
{
// To store the count of even and
// odd number from the array
int cntEven = 0, cntOdd = 0;
for (int i = 0; i < n; i++) {
// If the current element is even
if (arr[i] % 2 == 0)
cntEven++;
// If it is odd
else
cntOdd++;
}
// To store the count of
// pairs with even sum
int evenPairs = 0;
// All the even elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntEven * (cntEven - 1)) / 2);
// All the odd elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
// To store the count of
// pairs with odd sum
int oddPairs = 0;
// All the even elements will make pairs
// with all the odd element and the
// sum of the pair will be odd
oddPairs += (cntEven * cntOdd);
cout << "Odd pairs = " << oddPairs << endl;
cout << "Even pairs = " << evenPairs;
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(int);
findPairs(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
static void findPairs(int arr[], int n)
{
// To store the count of even and
// odd number from the array
int cntEven = 0, cntOdd = 0;
for (int i = 0; i < n; i++)
{
// If the current element is even
if (arr[i] % 2 == 0)
cntEven++;
// If it is odd
else
cntOdd++;
}
// To store the count of
// pairs with even sum
int evenPairs = 0;
// All the even elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntEven * (cntEven - 1)) / 2);
// All the odd elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
// To store the count of
// pairs with odd sum
int oddPairs = 0;
// All the even elements will make pairs
// with all the odd element and the
// sum of the pair will be odd
oddPairs += (cntEven * cntOdd);
System.out.println("Odd pairs = " + oddPairs);
System.out.println("Even pairs = " + evenPairs);
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = arr.length;
findPairs(arr, n);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to find the count of pairs
# with odd sum and the count
# of pairs with even sum
def findPairs(arr, n) :
# To store the count of even and
# odd number from the array
cntEven = 0; cntOdd = 0;
for i in range(n) :
# If the current element is even
if (arr[i] % 2 == 0) :
cntEven += 1;
# If it is odd
else :
cntOdd += 1;
# To store the count of
# pairs with even sum
evenPairs = 0;
# All the even elements will make
# pairs with each other and the
# sum of the pair will be even
evenPairs += ((cntEven * (cntEven - 1)) // 2);
# All the odd elements will make
# pairs with each other and the
# sum of the pair will be even
evenPairs += ((cntOdd * (cntOdd - 1)) // 2);
# To store the count of
# pairs with odd sum
oddPairs = 0;
# All the even elements will make pairs
# with all the odd element and the
# sum of the pair will be odd
oddPairs += (cntEven * cntOdd);
print("Odd pairs = ", oddPairs);
print("Even pairs = ", evenPairs);
# Driver code
if __name__ == "__main__" :
arr = [ 1, 2, 3, 4, 5 ];
n = len(arr);
findPairs(arr, n);
# This code is contributed by kanugargng
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
static void findPairs(int []arr, int n)
{
// To store the count of even and
// odd number from the array
int cntEven = 0, cntOdd = 0;
for (int i = 0; i < n; i++)
{
// If the current element is even
if (arr[i] % 2 == 0)
cntEven++;
// If it is odd
else
cntOdd++;
}
// To store the count of
// pairs with even sum
int evenPairs = 0;
// All the even elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntEven * (cntEven - 1)) / 2);
// All the odd elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
// To store the count of
// pairs with odd sum
int oddPairs = 0;
// All the even elements will make pairs
// with all the odd element and the
// sum of the pair will be odd
oddPairs += (cntEven * cntOdd);
Console.WriteLine("Odd pairs = " + oddPairs);
Console.WriteLine("Even pairs = " + evenPairs);
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
findPairs(arr, n);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript implementation of the approach
// Function to find the count of pairs
// with odd sum and the count
// of pairs with even sum
function findPairs(arr, n) {
// To store the count of even and
// odd number from the array
let cntEven = 0, cntOdd = 0;
for (let i = 0; i < n; i++) {
// If the current element is even
if (arr[i] % 2 == 0)
cntEven++;
// If it is odd
else
cntOdd++;
}
// To store the count of
// pairs with even sum
let evenPairs = 0;
// All the even elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntEven * (cntEven - 1)) / 2);
// All the odd elements will make
// pairs with each other and the
// sum of the pair will be even
evenPairs += ((cntOdd * (cntOdd - 1)) / 2);
// To store the count of
// pairs with odd sum
let oddPairs = 0;
// All the even elements will make pairs
// with all the odd element and the
// sum of the pair will be odd
oddPairs += (cntEven * cntOdd);
document.write("Odd pairs = " + oddPairs + "<br>");
document.write("Even pairs = " + evenPairs);
}
// Driver code
let arr = [1, 2, 3, 4, 5];
let n = arr.length;
findPairs(arr, n);
// This code is contributed by _saurabh_jaiswal
</script>
Output: Odd pairs = 6
Even pairs = 4
Complexity Analysis:
- Time Complexity : O(N).
In every iteration of for loop, an element from either of the array is processed. Therefore, the time complexity is O(N). - Auxiliary Space : O(1).
Any extra space is not required, so the space complexity is constant.
Similar Reads
Count of even and odd power pairs in an Array Given an array arr[] of length N, the task is to count the number of pairs (X, Y) such that XY is even and count the number of pairs such that XY is odd.Examples: Input: arr[] = {2, 3, 4, 5} Output: 6 6 Explanation: (2, 3), (2, 4), (2, 5), (4, 2), (4, 3) and (4, 5) are the pairs with even values and
5 min read
Count of pairs having even and odd LCM from an array Given an array arr[] of size N, the task is to count the number of pairs having even LCM and odd LCM. Examples: Input: arr[] = {3, 6, 5, 4}Output: Even = 5, Odd = 1Explanation: LCM of (3, 6) is 6, LCM of (3, 5) is 15, LCM of (3, 4) is 12, LCM of (6, 5) is 30, LCM of (6, 4) is 12, LCM of (5, 4) is 20
6 min read
Count of adjacent pairs in given Array with even sum Given an array arr[] of N integers, the task is to find the count of pairs of adjacent elements whose sum is even where each element can belong to at most one pair. Example: Input: arr[] = {1, 12, 1, 3, 5}Output: 1Explanation: 1 pair can be formed with arr[3] and arr[4]. Input: arr[] = {1, 2, 3, 4,
4 min read
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 poss
7 min read
Program to print Sum of even and odd elements in an array Prerequisite - Array Basics Given an array, write a program to find the sum of values of even and odd index positions separately. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output :Even index positions sum 9 Odd index positions sum 12 Explanation: Here, n = 6 so there will be 3 even index position
13 min read