Question

In: Computer Science

Design and implement an algorithm that gets as input a list of k integer values N1,...

Design and implement an algorithm that gets as input a list of k integer values N1, N2, …., Nk, as well as a special value SUM. Your algorithm must locate a pair of values in the list N that sum to the value SUM. For example, If your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm would output either of the two values (2, 18) or (3, 17). If your algorithm cannot find any pair of values that sum to the value of SUM, then it should print the message ‘Sorry there is no such pair of values.’

Solutions

Expert Solution

please give thumbs up, thanks

sample output:

code:

#required funcion
def FindPair(Values,SUM):
hasfound=False;
for i in Values:
for j in Values:
if (i+j ==SUM):
hasfound=True;
a=i;
b=j;
break;
if(hasfound==True):
break;
if(hasfound==True):
print("(",a,",",b,")");
else:
print("Sorry there is no such pair of values.");
  
#main to call FindPair Function
def main():
Values=[3,8,13,2,17,18,10];
SUM=20;
FindPair(Values,SUM);
  
#callinf main functions
main()


Related Solutions

a) Design in pseudocode an algorithm that gets as input an integer k, and a list...
a) Design in pseudocode an algorithm that gets as input an integer k, and a list of k integers N1, .., Nk, as well as a special value SUM. Your algorithm must locate a pair of values in the list that sum to the value SUM. For example, if your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm should output "either" the two values 2 and 18,...
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of...
Write a MIPS program to implement the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 8 as...
In C ++, Design and implement a program that reads from the user four integer values...
In C ++, Design and implement a program that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:    95, 80, 100, 70 Highest grade: 100 Lowest grade:   70 Average grade: 86.25
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3...
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3 numbers a, b, c, from the user, and prints the 3 numbers in ascending order
Implement a Lucky Draw game in which the user has to input an integer ‘input’ and...
Implement a Lucky Draw game in which the user has to input an integer ‘input’ and multiply it with a random integer to get the product ‘p’. Depending upon the value of ‘p’ display the prize amount. Use a suitable Java Collection object to store the prize amount for each value of ‘p’. Design a generic stack data structure (not a Collection object) which can handle integer, double, character and any user-defined objects. Write a menu-driven Java program to test...
Write a program that first gets a list of integers from input. The input begins with...
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain fewer than 20 integers. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a space,...
Implement in C++ a GroceryListADT that does the following: gets the input about a grocery item...
Implement in C++ a GroceryListADT that does the following: gets the input about a grocery item (itemName, price, quantity, etc) from the user (from the keyboard)
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a...
Use quickselect algorithm to implement the two algorithms for finding the kth smallest integer in a set of integers. MUST USE JAVA AND QUICKSELECT. Algorithm 1: Procedure SELECT( k,S) { if ISI =1 then return the single element in S    else { choose an element randomly from S;               let S1, S2, and S3 be the sequences of elements in S                 less than, equal to, and greater than m, respectively;              if IS1I >=k then return SELECT(k,S1)              ...
1. Implement the faster algorithm for integer multiplication in C++ as a function, called “multiply”, that...
1. Implement the faster algorithm for integer multiplication in C++ as a function, called “multiply”, that takes 2 unsigned integers and returns their multiplication as an unsigned integer. 2. Test your code in main. Hints: Represent integers as strings. Write a utility function that pads integers with zeros, this will be useful If the 2 integers differ in length In calculating ??10^? and (??+??) 10^(?/2)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT