human
Can you provide a code to numbers of pairs from an array whose average is also present in the array and the other steps required given the following:
Numbers of pairs from an array whose average is also present in the array | Function to count the number of pairs from the array having sum S.
Stores the total count of pairs whose sum is 2 * S.
Generate all possible pairs and check their sums.
If the sum is S , then increment the count.
Return the total count of pairs.
Function to count of pairs having whose average exists in the array.
Initialize the count.
Use set to remove duplicates.
Add elements in the set.
For every sum , count all possible pairs.
Return the total count.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to numbers of pairs from an array whose average is also present in the array and the other steps required.
 def getCountPairs ( arr , N , S ) :
	count = 0
	for i in range ( len ( arr ) ) :
		for j in range ( i + 1 , len ( arr ) ) :
			if ( ( arr [ i ] + arr [ j ] ) == S ) :
				count += 1
	return count
def countPairs ( arr , N ) :
	count = 0
	S = set ( [ ] )
	for i in range ( N ) :
		S . add ( arr [ i ] )
	for ele in S :
		sum = 2 * ele
		count += getCountPairs ( arr , N , sum )
	return count
if __name__ == " _ _ main _ _ " :
	arr = [ 4 , 2 , 5 , 1 , 3 , 5 ]
	N = len ( arr )
	print ( countPairs ( arr , N ) )