- Related Questions & Answers
- 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 subarrays with same even and odd elements in C++
- Swapping even and odd index pairs internally in JavaScript
- Count rotations of N which are Odd and Even in C++
- Count odd and even digits in a number in PL/SQL
- Count even and odd digits in an Integer in C++
- Count Pairs from two arrays with even sum in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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.

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.

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

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

Even Product pairs: 6 Odd Product pairs: 6

Advertisements