Python - K Summation from Two lists

K summation from two lists means finding all pairs of elements where one element comes from the first list, another from the second list, and their sum equals a target value k. Python offers several approaches to solve this problem efficiently.

Using Brute Force Method

The brute force approach checks every possible combination of elements from both lists ?

def k_sum_naive(list1, list2, k):
    result = []
    for num1 in list1:
        for num2 in list2:
            if num1 + num2 == k:
                result.append((num1, num2))
    return result

list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
k = 10

print(f'The first list is: {list1}')
print(f'The second list is: {list2}')
print(f'The required pairs are: {k_sum_naive(list1, list2, k)}')
The first list is: [1, 2, 3, 4, 5]
The second list is: [6, 7, 8, 9, 10]
The required pairs are: [(1, 9), (2, 8), (3, 7), (4, 6)]
  • Time Complexity: O(n²)

  • Space Complexity: O(1)

Using Two-Pointer Approach

This optimized approach uses two pointers moving from opposite ends of sorted lists ?

def k_sum_two_pointer(list1, list2, k):
    list1.sort()
    list2.sort()
    result = []
    ptr1, ptr2 = 0, len(list2) - 1
    
    while ptr1 < len(list1) and ptr2 >= 0:
        current_sum = list1[ptr1] + list2[ptr2]
        if current_sum == k:
            result.append((list1[ptr1], list2[ptr2]))
            ptr1 += 1
            ptr2 -= 1
        elif current_sum < k:
            ptr1 += 1
        else:
            ptr2 -= 1
    return result

list1 = [1, 2, 3, 4, 5]
list2 = [12, 7, 8, 9, 10]
k = 13

print(f'The first list is: {list1}')
print(f'The second list is: {list2}')
print(f'The required pairs are: {k_sum_two_pointer(list1, list2, k)}')
The first list is: [1, 2, 3, 4, 5]
The second list is: [12, 7, 8, 9, 10]
The required pairs are: [(1, 12), (3, 10), (4, 9), (5, 8)]
  • Time Complexity: O(n log n) due to sorting

  • Space Complexity: O(1)

Using Hashing Method

Hash tables provide the most efficient solution by storing elements for O(1) lookup ?

def k_sum_hashing(list1, list2, k):
    hash_table = set(list1)  # Using set for O(1) lookup
    result = []
    
    for num2 in list2:
        complement = k - num2
        if complement in hash_table:
            result.append((complement, num2))
    return result

list1 = [1, 2, 3, 4, 5]
list2 = [12, 7, 8, 9, 10]
k = 13

print(f'The first list is: {list1}')
print(f'The second list is: {list2}')
print(f'The required pairs are: {k_sum_hashing(list1, list2, k)}')
The first list is: [1, 2, 3, 4, 5]
The second list is: [12, 7, 8, 9, 10]
The required pairs are: [(1, 12), (5, 8), (4, 9), (3, 10)]
  • Time Complexity: O(n)

  • Space Complexity: O(n)

Using List Comprehension

List comprehension provides a concise one-liner solution ?

def k_sum_list_comprehension(list1, list2, k):
    return [(num1, num2) for num1 in list1 for num2 in list2 if num1 + num2 == k]

list1 = [1, 2, 3, 4, 5]
list2 = [8, 5, 7, 4]
k = 9

print(f'The first list is: {list1}')
print(f'The second list is: {list2}')
print(f'The required pairs are: {k_sum_list_comprehension(list1, list2, k)}')
The first list is: [1, 2, 3, 4, 5]
The second list is: [8, 5, 7, 4]
The required pairs are: [(1, 8), (2, 7), (4, 5), (5, 4)]
  • Time Complexity: O(n²)

  • Space Complexity: O(1)

Using Itertools Library

The itertools.product() generates all combinations efficiently ?

import itertools

def k_sum_product(list1, list2, k):
    return [(num1, num2) for num1, num2 in itertools.product(list1, list2) if num1 + num2 == k]

list1 = [1, 2, 3, 4, 5]
list2 = [8, 5, 7, 4]
k = 9

print(f'The first list is: {list1}')
print(f'The second list is: {list2}')
print(f'The required pairs are: {k_sum_product(list1, list2, k)}')
The first list is: [1, 2, 3, 4, 5]
The second list is: [8, 5, 7, 4]
The required pairs are: [(1, 8), (2, 7), (4, 5), (5, 4)]
  • Time Complexity: O(n²)

  • Space Complexity: O(n²)

Comparison

Method Time Complexity Space Complexity Best For
Brute Force O(n²) O(1) Small datasets, simple implementation
Two Pointer O(n log n) O(1) Memory efficiency, sorted data
Hashing O(n) O(n) Large datasets, fastest solution
List Comprehension O(n²) O(1) Readable, Pythonic code
Itertools O(n²) O(n²) Built-in library approach

Conclusion

For optimal performance, use the hashing method with O(n) time complexity. For memory-constrained environments, choose the two-pointer approach. List comprehension offers the most readable solution for smaller datasets.

Updated on: 2026-03-27T08:32:56+05:30

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements