Count ways of choosing a pair with maximum difference in C++

C++Server Side ProgrammingProgramming

We are given with an array of numbers Arr[]. The goal is to count the number of pairs whose difference is equal to the maximum difference of all possible pairs. Count pairs (i!=j) and arr[x]- arr[y] is maximum possible.

We will do this by first finding the maximum difference where (i!=j). And store as maxdiff. Then count all those pairs that have difference=maxdiff.

Let’s understand with examples.

Input − arr[]= { 1,2,3,2,4,1,5 }

Output − No. of ways of choosing pair with maximum difference − 2

Explanation

Here minimum no. is 1 and maximum number is 5, maximum difference =5-1=4
Pair 1 [ 1,2,3,2,4,1,5 ] → (1,5) difference=4
Pair 2 [ 1,2,3,2,4,1,5 ] → (1,5) difference=4
Number of pairs with difference which is maximum=2.

Input − arr[]= { 2,4,2,4 }

Output − No. of ways of choosing pair with maximum difference − 4

Explanation

Here minimum no. is 2 and maximum number is 4, maximum difference =4-2=2
Pair 1 [ 2,4,2,4 ] → (2,4) difference=2
Pair 2 [ 2,4,2,4 ] → (2,4) difference=2
Pair 3 [ 2,4,2,4 ] → (4,2) difference=2
Pair 4 [ 2,4,2,4 ] → (2,4) difference=2
Number of pairs with difference which is maximum=4.

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 returns ways of choosing the pairs whose difference is equal to the maximum difference.

  • Take the initial variable count as 0 for the number of ways.

  • Take the variable diff as the difference of each pair.

  • Take the maxdiff variable as the maximum difference of all pairs.

  • Find maximum and minimum values from the array and store in maxx and mini respectively

  • Now maxdiff will be maxx-mini.

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

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

  • Calculate diff=arr[i]-arr[j] or arr[j]-arr[i] count as separate. If diff==maxdiff increment count as this pair has the maximum difference.

  • At the end of all loops count will have a total number of pairs that meet the condition.

  • Return the count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int countWays(int arr[],int n){
   int count = 0;
   int diff;
   int maxdiff=0; //making minimum as larger than any product in array
   int mini,maxx;
   mini=maxx=arr[0];
   for (int i = 0; i < n; i++) //find minimum and maximum values{
      if(arr[i]<mini)
         {mini=arr[i];}
      if(arr[i]>maxx)
         { maxx=arr[i]; }
   }
   maxdiff=maxx-mini; //this is maximum difference
   //cout<<maxx<<" "<<mini;
   for (int i = 0; i < n-1; i++){
      for (int j = i+1; j < n; j++){
         diff=arr[i]-arr[j]; //pair 1
         if ( diff==maxdiff ){
            count++;
            //cout<<endl<<"a :"<<arr[i]<<" b :"<<arr[j]; //to print
         }
         diff=arr[j]-arr[i]; //pair 2
         if ( diff==maxdiff ){
            count++;
            //cout<<endl<<"a :"<<arr[i]<<" b :"<<arr[j]; //to print
         }
      }
   }
   return count;
}
int main(){
   int Arr[]={ 3, 2, 1, 1, 3 };
   int N=5; //length of array
   cout <<endl<< "No. of ways of choosing pair with maximum difference : "<<countWays(Arr,N);
   return 0;
}

Output

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

No. of ways of choosing pair with maximum difference : 4
raja
Published on 29-Aug-2020 11:14:12
Advertisements