Peter received money from his parents this week and wants to spend it all buying books. But he doesnot read a book so fast, because he likes to enjoy every single word while he is reading. In this way, ittakes him a week to finish a book.As Peter receives money every two weeks, he decided to buy two books, then he can read them untilreceive more money. As he wishes to spend all the money, he should choose two books whose pricessummed up are equal to the money that he has. It is a little bit difficult to find these books, so Peterasks your help to find them.InputEach test case starts with 2 ≤ N ≤ 10000, the number of available books. Next line will have Nintegers, representing the price of each book, a book costs less than 1000001. Then there is anotherline with an integer M, representing how much money Peter has. There is a blank line after each testcase. The input is terminated by end of file (EOF).OutputFor each test case you must print the message: ‘Peter should buy books whose prices are i andj.’, where i and j are the prices of the books whose sum is equal do M and i ≤ j. You can consider thatis always possible to find a solution, if there are multiple solutions print the solution that minimizesthe difference between the prices i and j. After each test case you must print a blank line.Sample Input240 4080510 2 6 8 410Sample OutputPeter should buy books whose prices are 40 and 40.Peter should buy books whose prices are 4 and 6.
Question
Peter received money from his parents this week and wants to spend it all buying books. But he doesnot read a book so fast, because he likes to enjoy every single word while he is reading. In this way, ittakes him a week to finish a book.As Peter receives money every two weeks, he decided to buy two books, then he can read them untilreceive more money. As he wishes to spend all the money, he should choose two books whose pricessummed up are equal to the money that he has. It is a little bit difficult to find these books, so Peterasks your help to find them.InputEach test case starts with 2 ≤ N ≤ 10000, the number of available books. Next line will have Nintegers, representing the price of each book, a book costs less than 1000001. Then there is anotherline with an integer M, representing how much money Peter has. There is a blank line after each testcase. The input is terminated by end of file (EOF).OutputFor each test case you must print the message: ‘Peter should buy books whose prices are i andj.’, where i and j are the prices of the books whose sum is equal do M and i ≤ j. You can consider thatis always possible to find a solution, if there are multiple solutions print the solution that minimizesthe difference between the prices i and j. After each test case you must print a blank line.Sample Input240 4080510 2 6 8 410Sample OutputPeter should buy books whose prices are 40 and 40.Peter should buy books whose prices are 4 and 6.
Solution
This problem is a variation of the two-sum problem where you are given an array of integers and a target sum, and you need to find two numbers in the array that add up to the target sum. Here are the steps to solve this problem:
- Read the number of available books, N.
- Read the prices of the books into an array.
- Read the amount of money Peter has, M.
- Sort the array of book prices.
- Initialize two pointers, one at the beginning of the array (low) and one at the end of the array (high).
- While low is less than high:
- Calculate the sum of the book prices at the low and high pointers.
- If the sum is equal to M, then these are the two book prices Peter should choose. Store these prices and continue to the next pair.
- If the sum is less than M, increment the low pointer.
- If the sum is more than M, decrement the high pointer.
- After the loop, the stored prices are the prices of the books Peter should buy. If there are multiple solutions, this will be the one that minimizes the difference between the prices.
- Print the message: 'Peter should buy books whose prices are i and j.', where i and j are the stored prices.
Similar Questions
Ruben wants to buy a dictionary that costs $15, a dinosaur book that costs $16, and a children's cookbook that costs $6. He has saved $8 from his allowance. How much more money does Ruben need to buy all three books?
Rita earns 44 dollars each week working part-time at a bookstore. She earns one additional dollar for each book that she sells.Let A be the amount (in dollars) that Rita earns in a week if she sells B books.Write an equation relating A to B. Then use this equation to find the amount of money Rita earns if she sells 25 books.
Andrew bought two books for ₹ 1830. By selling one book at a profit of 7% and the other at a loss of 8% he neither gains nor loses in the whole transaction. Find the cost price of each book.₹ 1000, ₹ 830₹ 950, ₹ 880₹ 854, ₹ 976₹ 700, ₹ 1130
Ernest wants to buy 21 new books for his book collection. If the average price for a book is $20.45, about how much should he plan to spend? A. $400 B. $800 C. $500 D. $204.50
Kamlesh bought 65 books for Rs.1050 from one shop and 50 books for Rs.1020 from another. What is the average price he paid per book?Options :Rs. 36.40Rs.18.20Rs. 24Rs.18
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.