human
Count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] | Function to find number of unordered pairs.
Maximum element from the array.
Array to store the frequency of each element.
Stores the number of unordered pairs.
Store the frequency of each element.
Find the number of unordered pairs.
If the number j divisible by ith element is present in the array.
If the ith element of the array has frequency more than one.
Driver code"
Solution in Python:
assistant
#Here is code to count the number of pairs ( i , j ) such that either arr [ i ] is divisible by arr [ j ] or arr [ j ] is divisible by arr [ i ] and the other steps required.
 def freqPairs ( arr , n ) :
	max = arr [ 0 ]
	for i in range ( len ( arr ) ) :
		if arr [ i ] > max :
			max = arr [ i ]
	freq = [ 0 for i in range ( max + 1 ) ]
	count = 0
	for i in range ( n ) :
		freq [ arr [ i ] ] += 1
	for i in range ( n ) :
		for j in range ( 2 * arr [ i ] , max + 1 , arr [ i ] ) :
			if ( freq [ j ] >= 1 ) :
				count += freq [ j ]
		if ( freq [ arr [ i ] ] > 1 ) :
			count += freq [ arr [ i ] ] - 1
			freq [ arr [ i ] ] -= 1
	return count
if __name__ == ' _ _ main _ _ ' :
	arr = [ 3 , 2 , 4 , 2 , 6 ]
	n = len ( arr )
	print ( freqPairs ( arr , n ) )