Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone explain why I ' m getting this error. I need to input the values just like this. 4 1 5 3 5 6
Can someone explain why Im getting this error. I need to input the values just like this.
this is the code:
import java.util.;
class Main
static class Pair
int first;
int second;
Pairint first, int second
this.first first;
this.second second;
static Pair getCandidatePairint A int target
HashSet set new HashSet;
for int num : A
int complement target num;
if setcontainscomplement
return new Pairnum complement;
set.addnum;
return new Pair;
public static void mainString args
Scanner scanner new ScannerSystemin;
int t scannernextInt;
scanner.nextLine; Consume newline
for int i ; i t; i
String statusAndSize scannernextLinesplit;
int status IntegerparseIntstatusAndSize;
int size IntegerparseIntstatusAndSize;
int groceries new intsize;
String groceriesString scannernextLinesplit;
for int j ; j size; j
groceriesj IntegerparseIntgroceriesStringj;
int target scannernextInt;
scanner.nextLine; Consume newline
Pair pair;
if status
pair getCandidatePairSortedgroceries target;
else
pair getCandidatePairgroceries target;
if pairfirst && pair.second
System.out.printlnTest case # i : Spend target dollars by buying the groceries with Mathminpairfirst, pair.second dollars and Mathmaxpairfirst, pair.second dollars;
else
System.out.printlnTest case # i : No way you can spend exactly target dollars;
static Pair getCandidatePairSortedint A int target
int left right Alength ;
while left right
int sum Aleft Aright;
if sum target
return new PairAleft Aright;
else if sum target
left;
else
right;
return new Pair;
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