Print distinct absolute differences of all possible pairs from a given array
Last Updated :
22 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define Max 100005
void printUniqDif( int n, int a[])
{
bitset<Max> bset;
bitset<Max> diff;
for ( int i = 0; i < n; i++) {
bset.set(a[i]);
}
for ( int i = 0; i <= Max; i++) {
if (bset[i]) {
diff = diff | (bset >> i);
}
}
int X = bset.count();
if (X != n) {
cout << 0 << " " ;
}
for ( int i = 1; i <= Max; i++) {
if (diff[i]) {
cout << i << " " ;
}
}
}
int main()
{
int a[] = { 1, 4, 6 };
int n = sizeof (a) / sizeof (a[0]);
printUniqDif(n, a);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int Max = 100005 ;
static void printUniqDif( int n, int [] a)
{
int [] bset = new int [ 33 ];
int diff = 0 ;
for (var i = 0 ; i < n; i++)
bset[a[i]] = 1 ;
int d = 0 ;
for (var i = 0 ; i < 33 ; i++)
d = d | (bset[i] << i);
for (var i = 0 ; i < 33 ; i++)
{
if (bset[i] == 1 )
diff |= (d >> i);
}
int X = 0 ;
for ( int i = 0 ; i < 33 ; i++)
if (bset[i] == 1 )
X++;
String Y =Integer.toBinaryString(diff);
if (X != n)
System.out.print( "0 " );
for (var i = 1 ; i < Y.length(); i++)
{
if (Y.charAt(i) == '1' )
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int [] a = { 1 , 4 , 6 };
int n = a.length;
printUniqDif(n, a);
}
}
|
Python3
Max = 100005
def printUniqDif(n, a):
bset = [ 0 for i in range ( 33 )]
diff = 0
for i in range (n):
bset[a[i]] = 1
d = 0
for i in range ( 1 , 33 ):
d = d | (bset[i]<<i)
for i in range ( 33 ):
if (bset[i]):
diff = diff | d >> i
X, Y = bset.count( 1 ), str ( bin (diff)[ 2 :])
if (X ! = n):
print ( 0 , end = " " )
for i in range ( 1 , len (Y)):
if (Y[i] = = '1' ):
print (i, end = " " )
if __name__ = = '__main__' :
a = [ 1 , 4 , 6 ]
n = len (a)
printUniqDif(n, a)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static int Max = 100005;
static void printUniqDif( int n, int [] a)
{
int [] bset = new int [33];
int diff = 0;
for ( var i = 0; i < n; i++)
bset[a[i]] = 1;
int d = 0;
for ( var i = 0; i < 33; i++)
d = d | (bset[i] << i);
for ( var i = 0; i < 33; i++)
{
if (bset[i] == 1)
diff |= (d >> i);
}
int X = bset.Count(f => f == 1);
string Y = Convert.ToString(diff, 2);
if (X != n)
Console.Write( "0 " );
for ( var i = 1; i < Y.Length; i++)
{
if (Y[i] == '1' )
Console.Write(i + " " );
}
}
public static void Main( string [] args)
{
int [] a = {1, 4, 6};
int n = a.Length;
printUniqDif(n, a);
}
}
|
Javascript
let Max = 100005
function printUniqDif(n, a)
{
let bset = new Array(33).fill(0)
let diff = 0
for ( var i = 0; i < n; i++)
bset[a[i]] = 1
let d = 0
for ( var i = 0; i < 33; i++)
d = d | (bset[i] << i)
for ( var i = 0; i < 33; i++)
{
if (bset[i] == 1)
diff |= (d >> i)
}
let X = bset.filter(x => x == 1).length
let Y = diff.toString(2)
if (X != n)
process.stdout.write( "0 " )
for ( var i = 1; i < Y.length; i++)
if (Y.charAt(i) == '1' )
process.stdout.write(i + " " )
}
let a = [1, 4, 6]
let n = a.length
printUniqDif(n, a)
|
Time Complexity:O(N + Max), where Max is the largest element of the array.
Auxiliary Space: O(Max)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...