Open In App

Python – Dictionary Keys whose Values summation equals K

Last Updated : 02 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionary and a value K, extract keys whose summation of values equals K.

Input : {“Gfg” : 3, “is” : 5, “Best” : 9, “for” : 8, “Geeks” : 10}, K = 17 
Output : [‘Best’, ‘for’] 
Explanation : 9 + 8 = 17, hence those keys are extracted. 

Input : {“Gfg” : 3, “is” : 5, “Best” : 9, “for” : 8, “Geeks” : 10}, K = 19 
Output : [‘Best’, ‘Geeks’] 
Explanation : 9 + 10 = 19, hence those keys are extracted.

Method #1 : Using loop

This is one of the ways in which this task can be performed. In this, we iterate for all the keys, and next keys in inner list, and keep on checking values summation. If its equal to K. The keys are stored.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys whose Values summation equals K
# Using loop
 
# initializing dictionary
test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 14
 
# extracting keys and values
keys = list(test_dict.keys())
values = list(test_dict.values())
 
res = None
for i in range(len(keys)):
    for j in range(i + 1, len(keys)):
 
        # checking if values equals K
        if values[i] + values[j] == K:
            res = [keys[i], keys[j]]
 
# printing result
print("Keys whose sum equals K : " + str(res))


Output

The original dictionary is : {'Gfg': 3, 'is': 5, 'Best': 9, 'for': 8, 'Geeks': 10}
Keys whose sum equals K : ['is', 'Best']

Time complexity: O(n^2), where n is the number of elements in the dictionary.
Auxiliary Space: O(1), the space used is constant regardless of the size of the input dictionary.

Method #2 : Using list comprehension 

This is yet another way in which this task can be performed. In this, we perform task similar to above method but in shorthand manner using list comprehension.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys whose Values summation equals K
# Using list comprehension
 
# initializing dictionary
test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 14
 
# extracting keys and values
keys = list(test_dict.keys())
values = list(test_dict.values())
 
# checking for keys in one liner
res = [[keys[i], keys[j]]  for i in range(len(keys)) for j in range(i + 1, len(keys)) if values[i] + values[j] == K]
 
# printing result
print("Keys whose sum equals K : " + str(res))


Output

The original dictionary is : {'Gfg': 3, 'is': 5, 'Best': 9, 'for': 8, 'Geeks': 10}
Keys whose sum equals K : [['is', 'Best']]

The time complexity of the given code is O(n^2), where n is the number of keys in the dictionary.

The space complexity of the code is O(n), where n is the number of keys in the dictionary. 

Method #3 : Using orderedDict + two pointer approach

In this approach, we first create an ordered dictionary with keys as values and values as keys. Next we use two pointer approach, one starting from 0 and other from the last index of ordered dictionary. We keep on checking if the sum of values at these two pointers is equal to K. If yes, we store the keys corresponding to these values as the result.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys whose Values summation equals K
# Using ordered dict and two pointer approach
 
from collections import OrderedDict
 
# initializing dictionary
test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10}
   
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
   
# initializing K
K = 14
 
# convert to ordered dict
temp_dict = OrderedDict(sorted(test_dict.items(), key=lambda x: x[1]))
 
# extract keys and values
keys = list(temp_dict.keys())
values = list(temp_dict.values())
 
res = []
l, r = 0, len(values)-1
 
while l < r:
    if values[l] + values[r] == K:
        res.append([keys[l], keys[r]])
        l += 1
        r -= 1
    elif values[l] + values[r] < K:
        l += 1
    else:
        r -= 1
 
# printing result
print("Keys whose sum equals K : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary is : {'Gfg': 3, 'is': 5, 'Best': 9, 'for': 8, 'Geeks': 10}
Keys whose sum equals K : [['is', 'Best']]

This approach uses an ordered dictionary to sort the values in increasing order and then uses a two pointer approach to check if the values at the two pointers sum up to K. The time complexity of this approach is O(nlogn) and space complexity is O(n) as we are storing the keys and values in separate lists. This approach is more efficient than the above ones as it avoids nested loops and unnecessary dictionary traversals.

Method #4: Using Dictionary Comprehension and filter()

Using a dictionary comprehension to iterate over the keys in the dictionary and checking if the summation of the current key’s value and any other key’s value (except itself) in the dictionary equals K. We are then storing the keys in a list called res.

Python3




# Python3 code to demonstrate working of
# Dictionary Keys whose Values summation equals K
# Using dictionary comprehension and filter()
 
# initializing dictionary
test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 14
 
# using dictionary comprehension and filter to find the keys
res = [key for key in test_dict.keys() if any(test_dict[key] + test_dict[other_key] == K for other_key in test_dict.keys() if other_key != key)]
 
# printing result
print("Keys whose sum equals K : " + str(res))


Output

The original dictionary is : {'Gfg': 3, 'is': 5, 'Best': 9, 'for': 8, 'Geeks': 10}
Keys whose sum equals K : ['is', 'Best']

Time complexity: O(n^2), where n is the number of keys in the dictionary. 
Auxiliary space: O(1). We are not using any additional data structures to store the results.

Method #5: Using defaultdict and loop

Step-by-step approach:

  • Import the defaultdict module from the collections library.
  • Initialize a defaultdict object.
  • Iterate over the keys in the dictionary.
    • For each key, calculate the difference between K and the value of the key.
    • If the difference exists as a value in the dictionary, add the current key and the corresponding key that adds up to K to the result list.
  • Return the result list.

Below is the implementation of the above approach:

Python3




from collections import defaultdict
 
test_dict = {"Gfg" : 3, "is" : 5, "Best" : 9, "for" : 8, "Geeks" : 10}
 
def find_keys(test_dict, K):
    result = []
    values_dict = defaultdict(list)
    for key, value in test_dict.items():
        values_dict[value].append(key)
        diff = K - value
        if diff in values_dict:
            for other_key in values_dict:
                if other_key != key:
                    result.append((key, other_key))
    return result
 
K = 14
print(find_keys(test_dict, K))


Output

[('Best', 'is')]

Time complexity: O(n)
Auxiliary space: O(n)

Method #6: Using set and intersection

  1. Initialize an empty set called seen and an empty list called result.
  2. Loop through each key-value pair in the dictionary:
    1. Calculate the difference between K and the current value.
    2. If the difference is present in the seen and append a tuple of the current key and the other key (whose value is the difference) to ‘result’
    3. Add the current key to the ‘seen’ set
  3. Print the result.

Below is the implementation of the above approach:

Python3




# Python program for the above approach
 
# Function to find the keys
def find_keys(test_dict, K):
    seen = set()
    result = []
 
    # Iterate the dictionary
    for key, value in test_dict.items():
        diff = K - value
        if diff in seen:
            result.append(
                (key, [k for k, v in test_dict.items() if v == diff][0]))
        seen.add(value)
 
    # Return the result
    return result
 
 
# Driver Code
test_dict = {"Gfg": 3, "is": 5, "Best": 9, "for": 8, "Geeks": 10}
K = 14
 
print(find_keys(test_dict, K))


Output

[('Best', 'is')]

Time complexity: O(N) where N is the number of key-value pairs in the dictionary
Auxiliary space: O(N) where N is the number of key-value pairs in the dictionary (for the ‘seen’ set)

Method#7: Using Recursive method.

Algorithm:
1. Initialize a list of keys and a list of values from the given dictionary.
2. If the length of the keys list is less than 2, return None.
3. Initialize a variable res to None.
4. Iterate over the range from 1 to the length of the keys list.
   a. Check if the sum of the value at index 0 and the value at the current index equals K.
   b. If so, set res to a list containing the key at index 0 and the key at the current index and break the loop.
5. If res is still None, call the function recursively with a new dictionary obtained by zipping the keys and values lists from index 1 to the end.
6. Return res.

Python3




def find_keys_with_sum(test_dict, K):
    keys = list(test_dict.keys())
    values = list(test_dict.values())
     
    if len(keys) < 2:
        return None
     
    res = None
    for i in range(1, len(keys)):
        if values[0] + values[i] == K:
            res = [keys[0], keys[i]]
            break
     
    if res is None:
        res = find_keys_with_sum(dict(zip(keys[1:], values[1:])), K)
     
    return res
test_dict = {"Gfg": 3, "is": 5, "Best": 9, "for": 8, "Geeks": 10}
K = 14
 
print(find_keys_with_sum(test_dict, K))


Output

['is', 'Best']

The time complexity of the recursive function is O(n^2), where n is the number of keys in the dictionary, because in the worst case (when no pair of keys has the target sum), the function will iterate over all possible pairs of keys.

 The space complexity of the function is also O(n^2), because it creates a new dictionary for each recursive call. However, in practice, the space complexity is likely to be much less than O(n^2) because of Python’s optimization of memory usage.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads