Python Program to Find a Pair with a Minimum Absolute Sum in an List

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

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: