- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

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

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

#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

- Related Questions & Answers
- Find pair with maximum difference in any column of a Matrix in C++
- Find Maximum difference pair in Python
- Probability of a random pair being the maximum weighted pair in C++
- Find a pair with maximum product in array of Integers in C++
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Find a pair with the given difference in C++
- Maximum Length of Pair Chain in C++
- Find the Pair with a Maximum Sum in a Matrix using C++
- Maximum XOR value of a pair from a range in C++
- Find Sum of pair from two arrays with maximum sum in C++
- Find pair with maximum GCD in an array in C++
- Count ways to spell a number with repeated digits in C++
- Find the Number of Ways to Pair People using C++
- Count the Number of matching characters in a pair of Java string
- Dynamically choosing a column in MySQL?