Python Program to get all possible differences between set elements
Last Updated :
02 May, 2023
Given a set, the task is to write a Python program to get all possible differences between its elements.
Input : test_set = {1, 5, 2, 7, 3, 4, 10, 14}
Output : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Explanation : All possible differences are computed.
Input : test_set = {1, 5, 2, 7}
Output : {1, 2, 3, 4, 5, 6}
Explanation : All possible differences are computed.
Method #1 : Using combinations() + abs() + loop
In this, all the possible pairs are extracted using combinations(). Then loop is used to traverse and abs() is used to get difference.
Python3
# Python3 code to demonstrate working of
# All elements difference in Set
# Using combinations + abs() + loop
from itertools import combinations
# initializing strings set
test_set = {1, 5, 2, 7, 3, 4, 10, 14}
# printing original string
print("The original set is : " + str(test_set))
# getting combinations
combos = combinations(test_set, 2)
res = set()
# adding differences in set
for x, y in combos:
res.add(abs(x - y))
# printing result
print("All possible differences : " + str(res))
Output:
The original set is : {1, 2, 3, 4, 5, 7, 10, 14}
All possible differences : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Method #2 : Using set comprehension + combinations() + abs()
In this, we perform task of getting and setting all elements using set comprehension as one liner approach to solve the problem.
Python3
# Python3 code to demonstrate working of
# All elements difference in Set
# Using set comprehension + combinations() + abs()
from itertools import combinations
# initializing strings set
test_set = {1, 5, 2, 7, 3, 4, 10, 14}
# printing original string
print("The original set is : " + str(test_set))
# set comprehension providing concise solution
res = {abs(x - y) for x, y in combinations(test_set, 2)}
# printing result
print("All possible differences : " + str(res))
Output:
The original set is : {1, 2, 3, 4, 5, 7, 10, 14}
All possible differences : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Method #3: Using nested loops
Using nested loops to iterate over all possible pairs of elements in the set. We compute the absolute difference between each pair and add it to a set. Finally, we return the set of differences.
Algorithm:
1. Create an empty set to store the differences.
2. Use nested loops to iterate over all possible pairs of elements in the set.
3. Compute the absolute difference between each pair of elements.
4. Add the difference to the set of differences.
5. Return the set of differences.
Python3
def get_differences1(test_set):
differences = set()
for x in test_set:
for y in test_set:
differences.add(abs(x-y))
differences.remove(0)
return differences
test_set = {1, 5, 2, 7, 3, 4, 10, 14}
print(get_differences1(test_set))
Output{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Time complexity: O(n^2), where n is the size of the set.The remove() function is a constant time operation and does not affect the time complexity.
Auxiliary Space: O(n), where n is the size of the set
Method #5: Using numpy array and broadcasting
- Convert the set test_set into a numpy array using numpy.array().
- Calculate the absolute difference between the array and its transpose using numpy broadcasting.
- Flatten the resulting array using numpy.ravel() and convert it into a set to remove duplicates.
- Print the resulting set of differences.
Python3
import numpy as np
# initializing strings set
test_set = {1, 5, 2, 7, 3, 4, 10, 14}
# convert set to numpy array
arr = np.array(list(test_set))
# calculate absolute differences using broadcasting
diffs = np.abs(arr - arr[:, np.newaxis])
# flatten and convert to set
res = set(np.ravel(diffs))
# remove 0 from set of differences
res.discard(0)
# print result
print("All possible differences : " + str(res))
OUTPUT :
All possible differences : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Time complexity: O(n^2), where n is the length of the test_set.
Auxiliary space: O(n^2), for the intermediate array.
Similar Reads
Python Program for Maximum difference between groups of size two
Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : ar
3 min read
Python program to find sum of absolute difference between all pairs in a list
Given a list of distinct elements, write a Python program to find the sum of absolute differences of all pairs in the given list.Examples: Input : [9, 2, 14] Output : 24 Explanation: (abs(9-2) + abs(9-14) + abs(2-14)) Input : [1, 2, 3, 4] Output : 10 Explanation: (abs(1-2) + abs(1-3) + abs(1-4) + ab
5 min read
Python program to extract rows with common difference elements
Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
3 min read
Python Program to remove elements that are less than K difference away in a list
Given a list, perform removal of those elements whose difference is less than K from its previous element. Input : test_list = [3, 19, 5, 8, 10, 13], K = 4Â Output : [3, 8, 13, 19]Â Explanation : 5 - 3 = 2, 2<4, hence 5 is removed, similarly, 10 - 8 is 2, less than K. Input : test_list = [15, 7, 20
3 min read
Python Program to Find a pair with the given difference
Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78 Output: Pair Found: (2, 80) Input: arr[] = {90, 70, 20, 80, 50}, n = 45 Output: No Such Pair Recommended: Please solve it on "PRA
4 min read
Python program to find all possible pairs with given sum
Given a list of integers and an integer variable K, write a Python program to find all pairs in the list with given sum K. Examples: Input : lst =[1, 5, 3, 7, 9] K = 12 Output : [(5, 7), (3, 9)] Input : lst = [2, 1, 5, 7, -1, 4] K = 6 Output : [(2, 4), (1, 5), (7, -1)] Method #1: Pythonic Naive This
7 min read
Python - Check if all tuples have element difference less than K
Given a Tuple list, check if each tuple has a difference less than K. Input : test_list = [(3, 4), (1, 2), (7, 8), (9, 13)], K = 2 Output : False Explanation : 13 - 9 = 4 > 2. Input : test_list = [(3, 4), (1, 2), (7, 8)], K = 2Output : True Explanation : All have abs. diff 1 < 2. Method #1 :
7 min read
Python Program to Sort Matrix Rows by summation of consecutive difference of elements
Given a Matrix, the following article depicts how to sort rows of a matrix on the basis of summation of difference between consecutive elements of a row. Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], Output : [[4, 3, 2, 1], [7, 2, 4, 5], [1, 5, 3, 6], [6, 9, 3, 2]] Ex
7 min read
Calculate Difference between Adjacent Elements in Given List - Python
The task of calculating the difference between adjacent elements in a list involves iterating through the list and computing the difference between each consecutive pair. For example, given a list a = [5, 4, 89, 12, 32, 45], the resulting difference list would be [-1, 85, -77, 20, 13],Using numpy Nu
3 min read
Difference between two Lists in Python
The difference between two lists in Python refers to the elements that are present in one list but not in the other. For example, finding the difference between lists a = [1, 2, 3, 4] and b = [3, 4, 5, 6] can result in [1, 2] by removing the common elements (3 and 4).Using setSet operations are most
3 min read