Count number of ordered pairs with Even and Odd Sums 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 sum 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 sum, if it is even increment count by 2 for even sums else increment count by 2 for odd sums.

Let’s understand with examples.

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

Output− Count of even product sums − 6 Count of odd sum pairs − 6

Explanation − Valid odd sum 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 sum 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 sum pairs − 6 Count of odd sum 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 sum 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 sums.

  • 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 sum 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 sum pairs by 2.

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

  • 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 sum pairs
   int count2=0; //odd sum pairs
   int sum=0;
   for(int i=0;i<n-1;i++){
      for(int j=i+1;j<n;j++){
         sum=arr[i]+arr[j];
         if(sum%2==0) //sum is even
            { count1+=2; } //(a,b) and (b,a) as two pairs
         else
            { count2+=2; }
      }
   }
   cout<<"Even Sum pairs: "<<count1;
   cout<<endl<<"Odd Sum pairs: "<<count2;
}
int main(){
   int arr[] = { 1,2,3,2 };
   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 Sum pairs: 4
Odd Sum pairs: 8
raja
Published on 29-Aug-2020 15:31:07
Advertisements