Question: Can someone explain the run - time of this program for both the sorted and unsorted array. import java.util. * ; class Main { static
Can someone explain the runtime of this program for both the sorted and unsorted array.
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
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
