Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6560
@blackshadows 6400
@mb1973 5777
@Quandray 5271
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4356
@sanjay05 3762
@mantu_singh 3638
@gfgaccount 3601
@marius_valentin_dragoi 3525
Complete Leaderboard
Count the pairs with maximum difference
Easy Accuracy: 52.96% Submissions: 151 Points: 2

Given an array a[ ] of N elements, the task is to find the number of ways to choose pairs {a[i], a[j]} such that their absolute difference is maximum.

Example:

Input:
N = 5
a[] = {3, 2, 1, 1, 3}
Output:
4
Explanation:
The maximum difference you can find is 2
which is from 3 - 1 = 2.
Number of ways of choosing it:
1) Choosing the first and third element
2) Choosing the first and fourth element
3) Choosing the third and fifth element
4) Choosing the fourth and fifth element
Hence, the answer is 4.

Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function countPairs() that take array(a) and sizeOfArray(N) as input parameters, and returns the count of pairs which has the maximum difference. The driver code takes care of the printing.

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 105
1 ≤ A[i] ≤ 105

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Count the pairs with maximum difference

Output Window