Print distinct absolute differences of all possible pairs from a given array | Python3 program for the above approach ; Function to find all distinct absolute difference of all possible pairs of the array ; bset [ i ] : Check if i is present in the array or not ; diff [ i ] : Check if there exists a pair whose absolute difference is i ; Traverse the array , arr [ ] ; Iterate over the range [ 0 , Max ] ; If i - th bit is set ; Insert the absolute difference of all possible pairs whose first element is arr [ i ] ; Stores count of set bits ; If there is at least one duplicate element in arr [ ] ; Printing the distinct absolute differences of all possible pairs ; If i - th bit is set ; Driver Code ; Given array ; Given size ; Function Call
Max = 100005 NEW_LINE def printUniqDif ( n , a ) : NEW_LINE INDENT bset = [ 0 for i in range ( 33 ) ] NEW_LINE diff = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT bset [ a [ i ] ] = 1 NEW_LINE DEDENT d = 0 NEW_LINE for i in range ( 1 , 33 ) : NEW_LINE INDENT d = d | ( bset [ i ] << i ) NEW_LINE DEDENT for i in range ( 33 ) : NEW_LINE INDENT if ( bset [ i ] ) : NEW_LINE INDENT diff = diff | d >> i NEW_LINE DEDENT DEDENT X , Y = bset . count ( 1 ) , str ( bin ( diff ) [ 2 : ] ) NEW_LINE if ( X != n ) : NEW_LINE INDENT print ( 0 , end = " ▁ " ) NEW_LINE DEDENT for i in range ( 1 , len ( Y ) ) : NEW_LINE INDENT if ( Y [ i ] == '1' ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 4 , 6 ] NEW_LINE n = len ( a ) NEW_LINE printUniqDif ( n , a ) NEW_LINE DEDENT