Count of index pairs with equal elements in an array in C++


We are given with an array of N elements. The goal is to find the index pairs (i,j) which have the same element value such that i!=j. i.e, Arr[i]=Arr[j] and i!=j. This is used to make pairs of gloves of equal size. Out of N gloves only paired gloves are useful to sell.

We will do this by running two loops with 0<=i<n-1 and i<j<n. Compare for each pair of (i,j) if Arr[i]==Arr[j] && Arr[i]>0 && Arr[j]>0, and i!=j. If true, increment the count of such pairs and make these elements as -1 ( Arr[i]=Arr[j]= -1) to remove them from further checking as no gloves can have size -1.

Let’s understand with examples.

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

Output − Count of index pairs with equal elements − 2

Explanation

count=0, Arr[]= [ 4,3,2,1,2,4 ]
Arr[0]=Arr[5], 0!=5, count=1 Arr[0]=Arr[5]=-1 → [ -1,3,2,1,2,-1 ]
Arr[2]=Arr[4], 2!=4, count=2 Arr[2]=Arr[4]=-1 → [ -1,3,-1,1,-1,-1 ]
Now array has no new pairs with equal values, i!=j and > -1. Total pairs=2

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

Output − Count of index pairs with equal elements − 2

Explanation

count=0, Arr[]= [ 2,2,2,2,2 ]
Arr[0]=Arr[1], 0!=1, count=1 Arr[0]=Arr[1]=-1 → [ -1,-1,2,2,2 ]
Arr[2]=Arr[3], 2!=3, count=2 Arr[2]=Arr[3]=-1 → [ -1,-1,-1,-1,2 ]
Now array has no new pairs with equal values, i!=j and > -1. Total pairs=2

Approach used in the below program is as follows

  • We take an integer array Arr[] initialized with random numbers for size of gloves > 0.

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

  • Function countPairs(int arr[], int n) takes an array, its length as input and returns the pairs which have equal size and different indexes.

  • 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] and arr[j] are positive. If arr[i]==arr[j] then increment count. (i will never be equal to j according to conditions set in loop, no need to compare ).

  • Now set arr[i]=arr[j]=-1, to remove them from further comparisons.

  • At the end of all loops count will have a total number of pairs of gloves.

  • Return the count as result.

Example

 Live Demo

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count equal elements to make pair of gloves
int countPairs(int arr[], int n){
   int count = 0;
   for(int i=0;i<n-1;i++){
      for(int j=i+1;j<n;j++){
         if(arr[i]==arr[j] && arr[i]>0 && arr[j]>0){
            count++;
            arr[i]=arr[j]=-1;
         }
      }
   }
   return count;
}
int main(){
   int arr[] = { 1,2,4,2,1,2,4 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout <<"Pair of gloves ( Equal element pairs ):"<<countPairs(arr, n);
   return 0;
}

Output

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

Pair of gloves ( Equal element pairs ):3.

Updated on: 29-Aug-2020

305 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements