Related Articles
Print distinct absolute differences of all possible pairs from a given array
• Last Updated : 13 Jan, 2021

Given an array, arr[] of size N, the task is to find the distinct absolute differences of all possible pairs of the given array.

Examples:

Input: arr[] = { 1, 3, 6 }
Output: 2 3 5
Explanation:
abs(arr[0] – arr[1]) = 2
abs(arr[1] – arr[2]) = 3
abs(arr[0] – arr[2]) = 5

Input: arr[] = { 5, 6, 7, 8, 14, 19, 21, 22 }
Output: 1 2 3 5 6 7 8 9 11 12 13 14 15 16 17

Naive Approach: The simplest approach to solve this problem is to generate all possible pairs of the given array and insert the absolute difference of each pair in a Set. Finally, print all the elements of the set.
Time Complexity: O(N2 * log(N))
Auxiliary Space: O(N2)

Approach: The above approach can be optimized using Bitset. Follow the steps below to solve the problem:

• Initialize a Bitset, say bset, where bset[i] check if i is present in the array or not.
• Traverse the array arr[] and store all the array elements in the bset.
• Initialize a Bitset, say diff, where diff[i] stores if the absolute difference of there exists any pair in the array whose value equal to i or not.
• Find the largest element of the array, say Max
• Iterate over the range [0, Max]. In every ith iteration check if bset[i] is true or not. If found to be true, then insert the absolute difference of i with all other array elements using diff = diff | (bset >> i).
• Finally, iterate over the range [0, Max] and check if diff[i] is true or not. If found to be true, then print i.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` ` `  `#include ` `using` `namespace` `std; ` `#define Max 100005 ` ` `  `// Function to find all distinct ` `// absolute difference of all ` `// possible pairs of the array ` `void` `printUniqDif(``int` `n, ``int` `a[]) ` `{ ` ` `  `    ``// bset[i]: Check if i is present ` `    ``// in the array or not ` `    ``bitset bset; ` ` `  `    ``// diff[i]: Check if there exists a ` `    ``// pair whose absolute difference is i ` `    ``bitset diff; ` ` `  `    ``// Traverse the array, arr[] ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Add in bitset ` `        ``bset.set(a[i]); ` `    ``} ` ` `  `    ``// Iterate over the range[0, Max] ` `    ``for` `(``int` `i = 0; i <= Max; i++) { ` ` `  `        ``// If i-th bit is set ` `        ``if` `(bset[i]) { ` ` `  `            ``// Insert the absolute difference ` `            ``// of all possible pairs whose ` `            ``// first element is arr[i] ` `            ``diff = diff | (bset >> i); ` `        ``} ` `    ``} ` ` `  `    ``// Stores count of set bits ` `    ``int` `X = bset.count(); ` ` `  `    ``// If there is at least one ` `    ``// duplicate element in arr[] ` `    ``if` `(X != n) { ` ` `  `        ``cout << 0 << ``" "``; ` `    ``} ` ` `  `    ``// Printing the distinct absolute ` `    ``// differences of all possible pairs ` `    ``for` `(``int` `i = 1; i <= Max; i++) { ` ` `  `        ``// If i-th bit is set ` `        ``if` `(diff[i]) { ` `            ``cout << i << ``" "``; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Given array ` `    ``int` `a[] = { 1, 4, 6 }; ` ` `  `    ``// Given size ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` ` `  `    ``// Function Call ` `    ``printUniqDif(n, a); ` ` `  `    ``return` `0; ` `} `

## Python3

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

Output:

`2 3 5`

Time Complexity:O(N + Max), where Max is the largest element of the array.
Auxiliary Space: O(Max)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :