Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this JAVA Code program. Problem: Help Gina buy groceries! Gina got a prepaid card with W dollars on it . The

I need help with this JAVA Code program.
Problem: Help Gina buy groceries!
Gina got a prepaid card with W dollars on it. The grocery store has x unique groceries where each grocery item requires a unique number of dollars to buy it. Gina is allowed to buy only two grocery items and she wants to spend the full W dollar to buy two groceries so that no money will remain in the card. So, she needs to decide whether is it possible or not to buy two groceries that will cost exactly W dollars. Given an array of x distinct none zero values representing the dollars needed to buy the groceries. Also, given the target dollars W available on the card. Determine in O(n) time whether or not there exist two distinct points in the array that sum to W. The given array maybe sorted or may not be already sorted. It will be specified in the input whether it is sorted or not and you have to fulfill specific requirement based on the sorted status. (For example, if the array contained 1,3,6,7, and 9 and W =10, then the method you are to write should return dollar pair (3,7), since 3+7=10. It should return dollar pair (0,0) for the same array of dollars if T =17.)
Input Format (Your code must read from standard input (no file i/o is allowed))
The first line of the input will have a single positive integer n representing the number of test cases in the inputs. The next 2*n lines will contain the test cases, with two lines being used for each test case. The first value on the first line of each test case will be the sorted status (0 means unsorted, 1 means sorted). The next number in the line is x, the size of the array (the number of groceries in the grocery store). The rest of the line will contain x distinct none zero integers representing the dollars needed to buy, each separated by spaces. The second line of each test case will contain a single integer, W, the target for the problem (The number of dollars in the prepaid card). Here's an example input contents:
3
163679
10
03163
11
0432-743910
14
Output Format
For each test case, output a line with one of the following two formats:
Test case #m: Spend X dollars by buying groceries with x1 dollars and x2 dollars.
Test case #m: No way you can spend exactly X dollars.
where m (1<= m <= n), represents the appropriate test case. In the output x1 also must be less than x2
Example output for the above inputs:
Test case#1: Spend 10 dollars by buying the groceries with 3 dollars and 7 dollars.
Test case#2: No way you can spend exactly 11 dollars.
Test case#3: Spend 14 dollars by buying the groceries with 4 dollars and 10 dollars.
For finding the pair in the sorted array, you must implement the following function:
a._________ getCandidatePair(int A[], int target): This function receives an int
array and the target and returns the pair of ints from the array that can addup
to target. For this purpose, you may consider having another class and return an
object of that class. If a pair does not exist, the function should return the
pair as (0,0). Note that you are not allowed to print any information in this
function as part of the output.
If you see that a particular test case is not sorted (sorted status =0), your code must process it with O(n)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions