Question: PYTHON 3 FOLLOW INSTRUCTIONS Complete the function countPairsThatSumToK() to take in an array and a number K, and return the number of pairs in the
PYTHON 3
FOLLOW INSTRUCTIONS
Complete the function countPairsThatSumToK() to take in an array and a number K, and return the number of pairs in the array that add up to K.
You must do this in O(n) :) no points if you do a nested loop or anything that has a complexity of O(n^2) (i.e. cannot use the in operator on an array inside a loop because the in operator for arrays takes O(n)).
Hint: use a data structure that allows O(1) lookup to store all the numbers from the array, and then loop over the array to see if for each item x, you can find another number (K-x) that helps produce a sum of K.

CODE:
def countPairsThatSumToK(array, K):
print(countPairsThatSumToK([1, 2, 3, 4, 6], 6)) # should print 1 print(countPairsThatSumToK([1, 2, 3, 4, 5], 5)) # should print 2
1 def countPairsThatSumTokCarray, K): 2 3 print(countPairsThatSumT0K([1, 2, 3, 4, 6], 6)) # should print 1 4 print(countPairsThatSumToKCC1, 2, 3, 4, 5], 5)) # should print 2 Problem Complete the function countPairsThatsumToK() to take in an array and a number K, and return the number of pairs in the array that add up to K. You must do this in O(n):) no points if you do a nested loop or anything that has a complexity of O(nA2) (i.e. cannot use the in operator on an array inside a loop because the in operator for arrays takes O(n) Hint: use a data structure that allows O(1) lookup to store all the numbers from the array, and then loop over the array to see if for each item x, you can find another number (K-x) that helps produce a sum of K
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
