- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

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

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

#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

- Related Articles
- Count number of ordered pairs with Even and Odd Sums in C++
- Count ordered pairs with product less than N in C++
- Count pairs with Bitwise AND as ODD number in C++
- Count pairs with Bitwise-AND as even number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Count pairs with Bitwise OR as Even number in C++
- Count pairs with Bitwise XOR as EVEN number in C++
- Count pairs with Odd XOR in C++
- Count number of even and odd elements in an array in C++
- Count odd and even digits in a number in PL/SQL
- Count subarrays with same even and odd elements in C++
- Swapping even and odd index pairs internally in JavaScript
- Number of pairs with Bitwise OR as Odd number in C++
- Count Pairs from two arrays with even sum in C++
- Check if product of digits of a number at even and odd places is equal in Python