Question: PYTHON 3 / Please complete problem! Problem Complete the function countPairsThatSumToK() to take in an array and a number K, and return the number of

PYTHON 3 / Please complete problem!

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(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.

PYTHON 3 / Please complete problem! Problem Complete the function countPairsThatSumToK() to

back to classrcom run Instructions from your teacher Problem Complete the function countPairsThatSumTok() to take in an array and a number K, and return the nnumber of pairs 1 ef countPairsThatSumTok (array, K): print(countPairsThat SumTok ([1, print(countPairsThatSumTok([1, 6)) 5)) # should # print print 6], 1 2 3 2, 3, 4, 4, 2, 3, 5], should In the nmay that add up to K. You must do this in O(u)) pls if yau do a wested loop or mything that as a cauplexily of 2) (i.e. catnol use Lhe in ?erlor un au amay insula loop because the in ?urilor lor arrays Lakes 0(n)). Hint: usc a data structure that allows O(1) loolup to store all the nubers from the aray, and then loop over the atray to sce if for each tan x. you can find another namber (K-X) that lelps produce a sum of K. Python 3.6.1 (detault, Dec 2615, 13:85:11) IGCC 4.8.2Linux

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!