Pairs that sum to k

Given an array of integers and a value k, print all pairs that sum to k.

Problem

Print all pairs in an array that sum up to a given value k.
Input:
k = 5
array = [1, 2, 3, 4, 5]
Output:
1,4
2,3

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by elrichendelgart
# Solution:
# The solution below is optimal because it uses a hash table to store the values in the array.
# The hash table is used to check if the complement of the current value is in the array.
# If the complement is in the array, then the pair is printed.
# The time complexity of this solution is O(n) because the hash table is iterated through once.
# The space complexity of this solution is O(n) because the hash table is the same size as the array.

def print_pairs(array, k):
    hash_table = {}
    for value in array:
        complement = k - value
        if complement in hash_table:
            print(value, complement)
        hash_table[value] = True

print_pairs([1, 2, 3, 4, 5], 5)

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a level of completeness. The approach is sound and the candidate has correctly identified the time and space complexity of the solution.

Evaluated at: 2022-11-08 00:15:37