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 ≤ 10^{5}
1 ≤ A[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: #User function Template for python3

def countPairs(a,n):
    return 0

Solutions:
def countPairs(a, n):
	if len(a) == 1:
		return 0
	maxi = a.count(max(a))
	mini = a.count(min(a))
	return maxi * mini


def countPairs(a, n):
	return a.count(max(a)) * a.count(min(a)) if n > 1 else 0


def countPairs(a, n):
	count1 = 0
	count2 = 0
	mini = min(a)
	maxi = max(a)
	for i in a:
		if i == mini:
			count1 += 1
		elif i == maxi:
			count2 += 1
	if mini == maxi:
		return n * (n - 1) // 2
	else:
		return count1 * count2


def countPairs(a, n):
	maximum = max(a)
	minimum = min(a)
	count1 = 0
	count2 = 0
	for i in a:
		if i == maximum:
			count1 += 1
		elif i == minimum:
			count2 += 1
	return count1 * count2


def countPairs(a, n):
	minn = min(a)
	maxx = max(a)
	count1 = 0
	count2 = 0
	for i in a:
		if i == maxx:
			count1 += 1
		if i == minn:
			count2 += 1
	if maxx == minn:
		return n * (n - 1) // 2
	else:
		return count1 * count2


def countPairs(a, n):
	a.sort()
	k = a[0]
	l = a[-1]
	if k == l:
		return n * (n - 1) // 2
	i = a.count(k)
	j = a.count(l)
	return i * j


def countPairs(a, n):
	a.sort(reverse=True)
	ma = 0
	j = n - 1
	c = 0
	for i in range(n):
		while abs(a[i] - a[j]) >= ma:
			ma = abs(a[i] - a[j])
			c += 1
			j -= 1
			if j < 0:
				if c == len(a):
					c = c * (c - 1)
					c = c // 2
					return c
				break
		j = n - 1
		if abs(a[i] - a[j]) < ma:
			break
	return c
	return 0


def countPairs(a, n):
	min_A = min(a)
	max_A = max(a)
	if min_A == max_A:
		return n * (n - 1) // 2
	count_max = 0
	count_min = 0
	for i in a:
		if i == max_A:
			count_max += 1
		if i == min_A:
			count_min += 1
	return count_min * count_max


def countPairs(a, n):
	if n == 1:
		return 0
	a.sort()
	r = {}
	for i in a:
		if i not in r:
			r[i] = 1
		else:
			r[i] += 1
	count = r[a[0]] * r[a[-1]]
	return count


def countPairs(a, n):
	a.sort()
	if n == 1:
		return 0
	return a.count(a[0]) * a.count(a[n - 1])
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = countPairs(a, n)
	print(ans)


def countPairs(a, n):
	minn = min(a)
	maxx = max(a)
	count1 = 0
	count2 = 0
	for i in a:
		if i == maxx:
			count1 += 1
		if i == minn:
			count2 += 1
	if maxx == minn:
		return n * (n - 1) // 2
	else:
		return count1 * count2
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = countPairs(a, n)
	print(ans)


def countPairs(a, n):
	if n == 1 or n == 2:
		return n - 1
	maxm = max(a)
	minm = min(a)
	maxm = a.count(maxm)
	minm = a.count(minm)
	return maxm * minm
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = countPairs(a, n)
	print(ans)


def countPairs(a, n):
	minn = min(a)
	maxx = max(a)
	cnt1 = 0
	cnt2 = 0
	for i in a:
		if i == maxx:
			cnt1 += 1
		if i == minn:
			cnt2 += 1
	if maxx == minn:
		return n * (n - 1) // 2
	else:
		return cnt1 * cnt2


def countPairs(a, n):
	if n == 1:
		return 0
		exit()
	set_a = set(a)
	diff = max(set_a) - min(set_a)
	i = 0
	for num in set_a:
		if num - diff in set_a:
			c1 = a.count(num)
			c2 = a.count(num - diff)
			i += c1 * c2
	return i


def countPairs(a, n):
	mx = -1
	mn = float('inf')
	for i in range(0, n):
		mx = max(mx, a[i])
		mn = min(mn, a[i])
	m = mx - mn
	c1 = 0
	c2 = 0
	for i in range(0, n):
		if a[i] == mx:
			c1 += 1
		if a[i] == mn:
			c2 += 1
	if m == 0:
		return c1 * (c2 - 1) // 2
	else:
		return c1 * c2


def countPairs(a, n):
	maxEle = max(a)
	minEle = min(a)
	countMax = a.count(maxEle)
	countMin = a.count(minEle)
	ways = countMax * countMin
	if len(a) == 1:
		return 0
	return ways


def countPairs(a, n):
	mn = +2147483647
	mx = -2147483648
	for i in range(n):
		mn = min(mn, a[i])
		mx = max(mx, a[i])
	c1 = 0
	c2 = 0
	for i in range(n):
		if a[i] == mn:
			c1 += 1
		if a[i] == mx:
			c2 += 1
	if mn == mx:
		return n * (n - 1) // 2
	else:
		return c1 * c2


def countPairs(a, n):
	dic = {}
	for i in a:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	if len(dic) == 1:
		return n * (n - 1) // 2
	else:
		return dic[max(a)] * dic[min(a)]
	return 0