
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find two pairs of numbers where difference between sum of these pairs are minimized in python
Suppose we have a list of numbers called nums and we want to select two pairs of numbers from it such that the absolute difference between the sum of these two pairs is minimized.
So, if the input is like nums = [3, 4, 5, 10, 7], then the output will be 1, as we can select these pairs (3 + 7) - (4 + 5) = 1.
To solve this, we will follow these steps:
- distances := a new list
- for i in range 0 to size of nums - 2, do
- for j in range i + 1 to size of nums - 1, do
- insert a list [|nums[i] - nums[j]| , i, j] at the end of distances
- sort the list distances
- ans := 1^9
- for i in range 0 to size of distances - 2, do
- [dist, i1, i2] := distances[i]
- j := i + 1
- [dist2, i3, i4] := distances[j]
- while j < size of distances and elements in (i1, i2, i3, i4) are not unique, do
- [dist2, i3, i4] := distances[j]
- j := j + 1
- if elements in (i1, i2, i3, i4) are unique, then
- ans := minimum of ans and (dist2 - dist)
- return ans
- for j in range i + 1 to size of nums - 1, do
Let us see the following implementation to get better understanding:
Example Code
class Solution: def solve(self, nums): distances = [] for i in range(len(nums) - 1): for j in range(i + 1, len(nums)): distances.append((abs(nums[i] - nums[j]), i, j)) distances.sort() ans = 1e9 for i in range(len(distances) - 1): dist, i1, i2 = distances[i] j = i + 1 dist2, i3, i4 = distances[j] while j < len(distances) and len({i1, i2, i3, i4}) != 4: dist2, i3, i4 = distances[j] j += 1 if len({i1, i2, i3, i4}) == 4: ans = min(ans, dist2 - dist) return ans ob = Solution() nums = [3, 4, 5, 10, 7] print(ob.solve(nums))
Input
[3, 4, 5, 10, 7]
Output
1
- Related Articles
- Python program to find sum of absolute difference between all pairs in a list
- Program to count number of permutations where sum of adjacent pairs are perfect square in Python
- Program to find max number of K-sum pairs in Python
- Program to count number of valid pairs from a list of numbers, where pair sum is odd in Python
- Program to Find K-Largest Sum Pairs in Python
- What are the two pairs of coprime and the two pairs of Twin prime numbers?
- Program to find XOR sum of all pairs bitwise AND in Python
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Find Maximum difference between tuple pairs in Python
- Maximum sum of pairs with specific difference C++ program
- Program to find lowest sum of pairs greater than given target in Python
- Program to find number of good pairs in Python
- Program to find array of doubled pairs using Python
- Program to find number of pairs where elements square is within the given range in Python
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python

Advertisements