# Count number of ordered pairs with Even and Odd Product in C++

C++Server Side ProgrammingProgramming

We are given an array of n positive numbers.The goal is to count the ordered pairs (arr[x], arr[y]) with the product of arr[x] and arr[y] is even or odd. Pair ( arr[i],arr[j] ) and ( arr[j],arr[i] are counted as separate.

We will traverse the array using two for loops for each number of pairs. Now calculate product, if it is even increment count by 2 for even products else increment count by 2 for odd products.

Let’s understand with examples.

Input− Arr[]= { 1,1,2,3 } N=4

Output − Count of even product pairs: 6 Count of odd product pairs: 6

Explanation − Valid odd product pairs are −

Arr[0] & Arr[1] → (1,1) Arr[1] & Arr[0] → (1,1) count=2
Arr[0] & Arr[3] → (1,3) Arr[3] & Arr[0] → (3,1) count=2
Arr[1] & Arr[3] → (1,3) Arr[3] & Arr[1] → (3,1) count=2 Total=6
Valid even product pairs are:
Arr[0] & Arr[2] → (1,2) Arr[2] & Arr[0] → (2,1) count=2
Arr[1] & Arr[2] → (1,2) Arr[2] & Arr[1] → (2,1) count=2
Arr[2] & Arr[3] → (2,3) Arr[3] & Arr[2] → (3,2) count=2 Total=6

Input − Arr[]= { 2,2,2 } N=3

Output − Count of even product pairs − 6 Count of odd product pairs − 0

Explanation − Valid even product pairs are −

Arr[0] & Arr[1] → (2,2) Arr[1] & Arr[0] → (2,2) count=2
Arr[1] & Arr[2] → (2,2) Arr[2] & Arr[1] → (2,2) count=2
Arr[2] & Arr[3] → (2,2) Arr[3] & Arr[2] → (2,2) count=2 Total=6
No odd product as all elements are even.

## Approach used in the below program is as follows

• We take an integer array arr[] initialized with random numbers.

• Take a variable n which stores the length of Arr[].

• Function countPairs(int arr[], int n) takes an array, its length as input and prints the count of pairs with even and odd products.

• Traverse array using two for loops for each element of the pair.

• Outer Loop from 0<=i<n-1, inner loop i<j<n

• Check if arr[i]*arr[j]%2==0. Increment count1 for count of even product pairs by 2 as arr[i],arr[j] and arr[j],arr[i] will be two pairs.

• If the above condition is false increment count2 for odd product pairs by 2.

• At the end of all loops count1 will have a total number of pairs that have even product and count2 will have a total number of pairs that have odd product

• Print the count1 and count2 as result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
void countPairs(int arr[], int n){
int count1=0; //even product pairs
int count2=0; //odd product pairs
int prod=1;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
prod=arr[i]*arr[j];
if(prod%2==0) //product is even
{ count1+=2; } //(a,b) and (b,a) as two pairs
else
{ count2+=2; }
}
}
cout<<"Even Product pairs: "<<count1;
cout<<endl<<"Odd Product pairs: "<<count2;
}
int main(){
int arr[] = { 1,2,7,3 };
int n = sizeof(arr) / sizeof(int);
countPairs(arr, n);
return 0;
}

## Output

If we run the above code it will generate the following output −

Even Product pairs: 6
Odd Product pairs: 6
Published on 29-Aug-2020 15:28:36
Advertisements