Given a list of distinct elements, write a Python program to find the sum of absolute differences of all pairs in the given list.
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) + abs(2-3) + abs(2-4) + abs(3-4))
The first approach is the brute force approach, which has been previously discussed. Here, we will discuss the pythonic approaches.
Approach #1 : Using
Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. In this method, we have a list ‘diffs’ which contains the absolute difference. We use two loops having two variables each. One to iterate through the counter and one for the list element. In every iteration, we check if the elements are similar or not. If not, find absolute difference and append it to diffs. Finally, find the sum of list. Since each pair will be counted twice, we divide the final sum by 2 and return it.
Approach #2 : Using itertools
Python itertools consist of permutation() method. This method takes a list as an input and return an object list of tuples that contain all permutation in a list form. Here, to find absolute difference we essentially need a permutation of two elements. Since each pair will be counted twice, we divide the final sum by 2.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Maximum absolute difference list of list
- Absolute Deviation and Absolute Mean Deviation using NumPy | Python
- Python program to find all possible pairs with given sum
- Python program to find the sum of all even and odd digits of an integer list
- Python | Find all distinct pairs with difference equal to k
- Check whether the sum of absolute difference of adjacent digits is Prime or not
- Python | Absolute value of list elements
- Python | Find Maximum difference between tuple pairs
- Python - Multiply all cross list element pairs
- Python - All possible pairs in List
- Python | Ways to sum list of lists and return sum list
- Python - Pairs with Sum equal to K in tuple list
- Python program to find all Strong Numbers in given list
- Python program to find all the Combinations in the list with the given condition
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python | Find all triplets in a list with given sum
- Python program to find Cumulative sum of a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.