Question: public final class Selector { private Selector ( ) { } public static int min ( int [ ] a ) { if ( a
public final class Selector
private Selector
public static int minint a
ifa null alength
throw new IllegalArgumentException;
if the first element in the array a is the minimum value
int min a;
example array
why i max
max ai;
return max;
public static int kminint a int k
ifa null alength k alength k
throw new IllegalArgumentException;
ifalength
return a;
int sorted new intalength;
sorted createCopysorted a;
sorted sortIncreasingsorted;
return searchForKsorted k;
public static int kmaxint a int k
ifa null alength k alength k
throw new IllegalArgumentException;
ifalength
return a;
int sorted new intalength;
sorted createCopysorted a;
sorted sortDecreasingsorted;
return searchForKsorted k;
Helper method for kmax only
private static int sortDecreasinginta
forint x ; x alength; x
forint y x ; y alength; y
ifax ay
int val ax;
ax ay;
ay val;
return a;
Helper method for kmin only
private static int sortIncreasinginta
forint x ; x alength; x
forint y x ; y alength; y
ifax ay
int val ax;
ax ay;
ay val;
return a;
Helper methods for kmax and kmin
private static int searchForKint arr, int k
ifk
return arr;
int index ;
forint n ; n arr.length ; n
ifarrn arrn
index;
ifk index
return arrn;
throw new IllegalArgumentException;
Helper method for kmin and kmax
private static int createCopyint arr, int copy
forint n ; n copy.length; n
arrn copyn;
return arr;
public static int rangeint a int low, int high
ifa null alength
throw new IllegalArgumentException;
int count ;
forint x ; x alength; x
ifax low && ax high
count;
int newArr new intcount;
int arrCount ;
forint x ; x alength; x
ifax low && ax high
newArrarrCount ax;
arrCount;
return newArr;
public static int ceilingint a int key
ifa null alength
throw new IllegalArgumentException;
int min ;
int exceptionCounter ;
forint x : a
ifx key
min x;
break;
exceptionCounter;
ifexceptionCounter alength
throw new IllegalArgumentException;
forint n : a
ifn key && n min
min n;
return min;
public static int floorint a int key
ifa null alength
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
