Count of odd and even sum pairs in an array

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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Odd pairs = 6
Even pairs = 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : Rajput-Ji, kanugargng