Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

**Lists in Python:**

Lists are one of Python’s most commonly used built-in data structures. You can make a list by putting all of the elements inside square brackets[ ] and separating them with commas. Lists can include any type of object, making them extremely useful and adaptable.

**Examples:**

**Example1:**

**Input:**

given list =[1, 12, 7, 10, 11, 13, 5, 4, 8, 4, 9, 3, 6, 5, 21, 8, 9]

**Output:**

The pair with minimum absolute sum in the given list [1, 12, 7, 10, 11, 13, 5, 4, 8, 4, 9, 3, 6, 5, 21, 8, 9] 1 3

- Python Program to Calculate the Average of a Number’s digits of Every Number in Given List
- Python Program to Calculate Sum of all Minimum Frequency Elements in Matrix
- Python Program to Print Neon numbers in a Range

**Example2:**

**Input:**

given list = [11, 23, 78, 5, 19, 23, 9, 78, 123]

**Output:**

The pair with minimum absolute sum in the given list [11, 23, 78, 5, 19, 23, 9, 78, 123] 5 9

Given a list, the task is to search a pair with the minimum absolute sum in the given list in Python.

## Program to Find a pair with a minimum absolute sum in a List in Python

Below is the full approach for finding a pair with the minimum absolute sum in the given list in Python.

### 1)Using While loop(Static Input)

**Idea:**

The aim is to keep search space by keeping two indexes (low and high) that originally point to two array endpoints. Then, if low is smaller than high, loop and decrease the search space list[low…high] at each loop iteration by comparing the sum of elements existing at low and high indexes with 0. If the total is less than zero, we increment the index low otherwise, we decrement index high if the sum is greater than zero. We also save the smallest absolute difference between all pairings present at low and high indexes.

**Input:**

Give the list as static input and store it in a variable.

Pass the given list to the findMinAbsSum function as an argument.

Print then pair which is having Minimum Absolute Sum in a given List.

The Exit of the Program.

**Below is the implementation:**

import sys # Function which accepts the given list as argument # which returns the pair in a list with an absolute minimum sum def findMinAbsSum(given_list): if len(given_list) < 2: return print("The pair with minimum absolute sum in the given list", given_list) # sort the given list if it is unsorted using sort() function given_list.sort() # keep two indexes pointing to the list's ends (lowptr, highptr) = (0, len(given_list) - 1) # minsum keeps track of the smallest absolute difference. minsum = sys.maxsize i = j = 0 # At each loop iteration, lower the search space given list[lowptr...highptr]. # If lowptr is less than highptr, loop using while loop while lowptr < highptr: # If the current absolute sum is less than the minimum, it will be updated. if abs(given_list[highptr] + given_list[lowptr]) < minsum: minsum = abs(given_list[highptr] + given_list[lowptr]) (i, j) = (lowptr, highptr) # Optimization: A pair with a zero-sum value is identified. if minsum == 0: break # f the sum is less than 0, increase the lowptr index. # If the total exceeds 0, reduce the highptr index. if given_list[highptr] + given_list[lowptr] < 0: lowptr = lowptr + 1 else: highptr = highptr - 1 # printing the pair print(given_list[i], given_list[j]) # Driver Code # Give the list as static input and store it in a variable. given_list = [1, 12, 7, 10, 11, 13, 5, 4, 8, 4, 9, 3, 6, 5, 21, 8, 9] # pass the given list as an argument to the findMinAbsSum function. findMinAbsSum(given_list)

**Output:**

The pair with minimum absolute sum in the given list [1, 12, 7, 10, 11, 13, 5, 4, 8, 4, 9, 3, 6, 5, 21, 8, 9] 1 3

### 2)Using While loop(User Input)

**Idea:**

The aim is to keep search space by keeping two indexes (low and high) that originally point to two array endpoints. Then, if low is smaller than high, loop and decrease the search space list[low…high] at each loop iteration by comparing the sum of elements existing at low and high indexes with 0. If the total is less than zero, we increment the index low otherwise, we decrement index high if the sum is greater than zero. We also save the smallest absolute difference between all pairings present at low and high indexes.

**Input:**

Give the list as user input using map(), split(), and list functions and store it in a variable.

Pass the given list to the findMinAbsSum function as an argument.

Print then pair which is having Minimum Absolute Sum in a given List.

The Exit of the Program.

**Below is the implementation:**

import sys # Function which accepts the given list as argument # which returns the pair in a list with an absolute minimum sum def findMinAbsSum(given_list): if len(given_list) < 2: return print("The pair with minimum absolute sum in the given list", given_list) # sort the given list if it is unsorted using sort() function given_list.sort() # keep two indexes pointing to the list's ends (lowptr, highptr) = (0, len(given_list) - 1) # minsum keeps track of the smallest absolute difference. minsum = sys.maxsize i = j = 0 # At each loop iteration, lower the search space given list[lowptr...highptr]. # If lowptr is less than highptr, loop using while loop while lowptr < highptr: # If the current absolute sum is less than the minimum, it will be updated. if abs(given_list[highptr] + given_list[lowptr]) < minsum: minsum = abs(given_list[highptr] + given_list[lowptr]) (i, j) = (lowptr, highptr) # Optimization: A pair with a zero-sum value is identified. if minsum == 0: break # f the sum is less than 0, increase the lowptr index. # If the total exceeds 0, reduce the highptr index. if given_list[highptr] + given_list[lowptr] < 0: lowptr = lowptr + 1 else: highptr = highptr - 1 # printing the pair print(given_list[i], given_list[j]) # Driver Code # Give the list as user input using map(), split(), and list # functions and store it in a variable. given_list = list(map(int, input( 'Enter some random elements to the list separated by spaces = ').split())) # pass the given list as an argument to the findMinAbsSum function. findMinAbsSum(given_list)

**Output:**

Enter some random elements to the list separated by spaces = 11 23 78 5 19 23 9 78 123 The pair with minimum absolute sum in the given list [11, 23, 78, 5, 19, 23, 9, 78, 123] 5 9

**Related Programs**:

- Python Program to Find the Sum of Elements in a List Recursively
- Python Program to Find the Second Largest Number in a List
- Python Program to Find the Cumulative Sum of a List
- Python Program to Print Sum of Negative Numbers, Positive Even Numbers, and Positive Odd numbers in a List
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Python Program to Find the Largest Number in a List