Given a array a[] of non-negative integers. Count the number of pairs (i, j) in the array such that a[i] + a[j] < a[i]*a[j]. (the pair (i, j) and (j, i) are considered same and i should not be equal to j)
Example 1:
Input:
N=3
arr[] = {3, 4, 5} 
Output: 3
Explanation: Pairs are (3, 4) , (4, 5) and (3,5).  
 
Example 2:
Input:
N=3
arr[] = {1, 1, 1 } 
Output: 0
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function CountPairs() that takes array arr and integer n as parameters and return the total number of pairs possible.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3


class Solution:
    def CountPairs (self,arr,n): 
        #code here.
      

Solutions:
from itertools import combinations as com

class Solution:

	def CountPairs(self, arr, n):
		work = sorted(arr)
		if 1 in work:
			work = work[n - work[::-1].index(1):]
		elif 0 in work:
			work = work[n - work[::-1].index(0):]
		z = work.count(2)
		v = len(work)
		x = v * (v - 1) // 2 - z * (z - 1) // 2
		return x
		return count


class Solution:

	def CountPairs(self, arr, n):
		twoCount = 0
		twoGrCount = 0
		for i in range(0, n):
			if arr[i] == 2:
				twoCount += 1
			elif arr[i] > 2:
				twoGrCount += 1
		return twoCount * twoGrCount + twoGrCount * (twoGrCount - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		(count, twocount) = (0, 0)
		for i in range(n):
			if arr[i] == 2:
				twocount += 1
			elif arr[i] > 2:
				count += 1
		return count * twocount + count * (count - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		p = n - arr.count(1)
		q = arr.count(2)
		res = p * (p - 1) // 2 - q * (q - 1) // 2
		return res


class Solution:

	def CountPairs(self, arr, n):
		count = 0
		total = 0
		zero = 0
		one = 0
		two = 0
		for i in range(n):
			if arr[i] == 0:
				zero += 1
			elif arr[i] == 1:
				one += 1
			elif arr[i] == 2:
				two += 1
		total = n - zero - one
		for i in range(n):
			if arr[i] > 1:
				if arr[i] == 2:
					count += total - two
				else:
					count += total - 1
		return count // 2


class Solution:

	def CountPairs(self, arr, n):
		count_zero_one = 0
		count_two = 0
		for item in arr:
			if item == 1 or item == 0:
				count_zero_one += 1
			if item == 2:
				count_two += 1
		non_zero_one = n - count_zero_one
		total_count = int(non_zero_one * (non_zero_one - 1) / 2)
		two_pairs = int(count_two * (count_two - 1) / 2)
		return total_count - two_pairs


class Solution:

	def CountPairs(self, arr, n):
		if n == 1:
			return 0
		c = n - sum((i < 2 for i in arr))
		if 2 in arr:
			if arr.count(2) == n:
				return 0
			co2 = arr.count(2)
			return c * (c - 1) // 2 - co2 * (co2 - 1) // 2 if c != 1 else 0
		return c * (c - 1) // 2 if c != 1 else 0


class Solution:

	def CountPairs(self, arr, n):
		cnt0 = 0
		twos = 0
		for i in arr:
			if i == 1:
				cnt0 += 1
			if i == 2:
				twos += 1
		return (n - cnt0) * (n - cnt0 - 1) // 2 - twos * (twos - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		c = 0
		d = 0
		for i in range(n):
			if arr[i] == 2:
				c = c + 1
			if arr[i] > 2:
				d = d + 1
		return c * d + d * (d - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		count_2 = 0
		count_g = 0
		for i in range(n):
			if arr[i] == 2:
				count_2 += 1
			if arr[i] > 2:
				count_g += 1
		return count_2 * count_g + count_g * (count_g - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		two = 0
		other = 0
		for i in arr:
			if i != 1 and i != 0:
				if i == 2:
					two += 1
				else:
					other += 1
		count = other * (other - 1) // 2
		if two > 0:
			count += two * other
		return count


class Solution:

	def CountPairs(self, arr, n):
		cnt1 = 0
		cnt2 = 0
		for i in range(n):
			if arr[i] == 2:
				cnt1 += 1
			elif arr[i] > 2:
				cnt2 += 1
		return cnt1 * cnt2 + cnt2 * (cnt2 - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		ones = arr.count(1)
		t = arr.count(2)
		if t == 0:
			t = 1
		a = n - ones
		if a == 0:
			return 0
		else:
			return a * (a - 1) // 2 - t * (t - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		two = 0
		twogr = 0
		for i in arr:
			if i == 2:
				two += 1
			elif i > 2:
				twogr += 1
		return two * twogr + twogr * (twogr - 1) // 2


class Solution:

	def CountPairs(self, arr, n):
		one_zero = 0
		two = 0
		for i in arr:
			if i < 2:
				one_zero += 1
			elif i == 2:
				two += 1
		greater_thenone = n - one_zero
		total_combinations = greater_thenone * (greater_thenone - 1) // 2
		sub_two = two * (two - 1) // 2
		return total_combinations - sub_two


class Solution:

	def CountPairs(self, arr, n):
		zero_one = 0
		two = 0
		for i in arr:
			if i < 2:
				zero_one += 1
			elif i == 2:
				two += 1
		count = n - zero_one
		pair = count * (count - 1) // 2
		sub_two = two * (two - 1) // 2
		return pair - sub_two


class Solution:

	def CountPairs(self, arr, n):
		arr.sort()
		l = 0
		c = 0
		r = n - 1
		while l < r:
			if a[l] + a[r] < a[l] * a[r]:
				c += r - l
				r -= 1
			else:
				l += 1
		return c


class Solution:

	def CountPairs(self, arr, n):
		(count_2, count_others) = (0, 0)
		for i in range(n):
			if arr[i] == 2:
				count_2 += 1
			elif arr[i] > 2:
				count_others += 1
		ans = count_2 * count_others + count_others * (count_others - 1) // 2
		return ans


class Solution:

	def CountPairs(self, arr, n):
		two = 0
		all = 0
		for i in arr:
			if i != 1:
				all += 1
			if i == 2:
				two += 1
		return (all * (all - 1) - two * (two - 1)) // 2
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	ob = Solution()
	print(ob.CountPairs(a, n))


class Solution:

	def CountPairs(self, arr, n):
		x = 0
		y = 0
		for i in arr:
			if i == 2:
				x += 1
			elif i > 2:
				y += 1
		ans = y * (y - 1) // 2 + x * y
		return ans
t = int(input())
for _ in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	ob = Solution()
	print(ob.CountPairs(a, n))


class Solution:

	def CountPairs(self, arr, n):
		x = 0
		y = 0
		for i in arr:
			if i == 2:
				x += 1
			elif i > 2:
				y += 1
		ans = y * (y - 1) // 2 + x * y
		return ans


class Solution:

	def CountPairs(self, arr, n):
		a = 0
		c = 0
		for i in arr:
			if i == 0 or i == 1:
				a += 1
			elif i == 2:
				c += 1
		k = n * (n - 1) // 2
		x = a * (a - 1) // 2
		y = a * (n - a)
		z = c * (c - 1) // 2
		return k - (x + y + z)


class Solution:

	def CountPairs(self, arr, n):
		for i in arr:
			if i == 1 or i == 0:
				n = n - 1
		x = 0
		y = arr.count(2)
		for i in range(1, n):
			x = x + i
		if y > 1:
			y = y * (y - 1) // 2
			return x - y
		return x