Python Program to Find all the unique pairs whose sum is equal to K

K Sum Pairs

Write a program to find all the unique pairs whose sum is equal to K.


The first line of input will contain comma-separated integers.
The second line of input will contain an integer (K).


The output should be M lines equal to the number of unique pairs sorted in ascending order.

Each line should contain a unique pair as a tuple with the smallest number first.


For example, if the given string is “5,3,7,9,5” and K is 12. Your code should print the unique pairs of numbers, whose sum is equal to K. In the above numbers

3+9 12
5+7 12

as these are tne unique pairs with sum equal to 12, print eacn pair as a tuple. So the output should be

(3, 9)
(5, 7)

Sample Input 1
5,3,7,9, 5

Sample Output 1
(3, 9)
(5, 7)


def get_unique_pairs(int_list, pair_sum):
    stop_index = len(int_list) - 1
    unique_pairs_set = set()
    for cur_index in range(stop_index):
        num_1 = int_list[cur_index]
        num_2 = pair_sum - num_1
        remaining_list = int_list[cur_index+1:]
        if num_2 in remaining_list:
            pair = (num_1, num_2)
            #avoid duplicates like (5,7) & (7,5)
            sorted_pair = tuple(sorted(pair))
    return unique_pairs_set
def convert_string_to_int(str_num_list):
    new_list = []
    for item in str_num_list:
        num = int(item)
    return new_list
str_num_list =input().split(",")
pair_sum = int(input())
int_list = convert_string_to_int(str_num_list)
unique_pairs = get_unique_pairs(int_list, pair_sum)
unique_pairs = list(unique_pairs)
unique_pairs.sort() #sort the tuples according to the first element
for pair in unique_pairs:

-4,-3,-2, -1,0,1,2,3,4,5,6

(-4, 3)
(-3, 2)
(-2, 1)
(-1, 0)

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published. Required fields are marked *