Question & Answer: Given an integer, k, and an array of integers, a, count the number of pairs of integers in a that sum to k. Complete the countPairs funct…..

using python
☆ Pairs That Sum to k Given an integer, k, and an array of integers, a, count the number of pairs of integers in a that sum to k Complete the countPairs function in the editor below. It has two parameters: 1. An integer, k, denoting the desired sum 2. An array of integers, a, denoting the values to examine More points will be awarded for solutions that can handle larger inputs within a set period of time i.e. code with a faster run-time complexity. Input Format The locked stub code in the editor reads the following input from stdin and passes it to the function The first line contains an integer, k, denoting the desired sum The second line contains an integer, n, denoting the size of the array, a Each line i of the n subsequent lines (where 0 s i s n) contains an integer describing element in a Constraints input array may contain duplicates. Output Format Your function must return an integer denoting the number of pairs of integers in a that sum to k This is printed to stdout by the locked stub code in your editor,

Given an integer, k, and an array of integers, a, count the number of pairs of integers in a that sum to k. Complete the countPairs function in the editor below. It has two parameters: 1. An integer, k, denoting the desired sum 2. An array of integers, a, denoting the values to examine More points will be awarded for solutions that can handle larger inputs within a set period of time i.e. code with a faster run-time complexity. The locked stub code in the editor reads the following input from stdin and passes it to the function: The first line contains an integer, k, denoting the desired sum. The second line contains an integer, n, denoting the size of the array, a. Each line i of the n subsequent lines (where 0 lessthanorequalto i

Expert Answer

 

This program requires nested loop inorder to find the pairs so complexity will be On2.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Given an integer, k, and an array of integers, a, count the number of pairs of integers in a that sum to k. Complete the countPairs funct…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

Please find below the solution and attached output:

Note : while pasting the code intendation (spacing) might be ommited as it is chegg limitation so please refer the attached code screenshot :

save below code as pair.py or any name needed please take care of itendation (spacing) as per python standard.

def find_pair(k,a):

“””

This program will find the

number of pair equalivalent to sum k

“””

#initialize count variable with 0

count=0

#nested looping through array a

for i in range(0,len(a)):

for j in range(i+1,len(a)):

#if equal to sum

if a[i] + a[j] == k:

#uncomment below line if u want to seek pair with its value

#print(“Pair is {“+str(a[i])+”, “+str(a[j])+”}”)

#increasing the count

count = count + 1

return count

#end of function

#empty list / array

a=[]

#asking sum

k= int(input(“Enter the value of k (desired sum) : “))

#askinh no of elements in array

n=int(input(“Enter an no of elements in array : “))

#using loop scanning no in array

for i in range(0,n):

a.append(int(input()))

if __name__ == “__main__”:

#finding pair by calling function

no_of_pairs = find_pair(k,a);

print(“nNo of pairs : “+str(no_of_pairs))

#Code screenshot:

Question & Answer: Given an integer, k, and an array of integers, a, count the number of pairs of integers in a that sum to k. Complete the countPairs funct..... 1

#Code Output:

Question & Answer: Given an integer, k, and an array of integers, a, count the number of pairs of integers in a that sum to k. Complete the countPairs funct..... 2

p

Still stressed from student homework?
Get quality assistance from academic writers!