Answered step by step
Verified Expert Solution
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 On 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 and and W then the method you are to write should return dollar pair since It should return dollar pair for the same array of dollars if T
Input Format Your code must read from standard input no file io 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 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 means unsorted, 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:
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 x dollars and x dollars.
Test case #m: No way you can spend exactly X dollars.
where m m n represents the appropriate test case. In the output x also must be less than x
Example output for the above inputs:
Test case#: Spend dollars by buying the groceries with dollars and dollars.
Test case#: No way you can spend exactly dollars.
Test case#: Spend dollars by buying the groceries with dollars and dollars.
For finding the pair in the sorted array, you must implement the following function:
a getCandidatePairint 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 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 your code must process it with On
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started