Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone please tell me why I ' m getting an array error for the following test case? 3 1 6 3 6 7 9
Can someone please tell me why Im getting an array error for the following test case?
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 scanner.nextInt;
scanner.nextLine; Consume newline
for int i ; i t; i
String statusAndSize scanner.nextLinesplit;
int status Integer.parseIntstatusAndSize;
int size Integer.parseIntstatusAndSize;
int groceries new intsize;
String groceriesString scanner.nextLinesplit;
for int j ; j size; j
groceriesj Integer.parseIntgroceriesStringj;
int target scanner.nextInt;
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 Math.minpairfirst, pair.second dollars and Math.maxpairfirst, 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