Question
int findCrossOver(int arr[], int low, int high, int x) { // Base cases if (arr[high] x) // x is smaller than all return low; //
int findCrossOver(int arr[], int low, int high, int x) { // Base cases if (arr[high] <= x) // x is greater than all return high; if (arr[low] > x) // x is smaller than all return low;
// Find the middle point int mid = (low + high)/2; /* low + (high - low)/2 */
/* If x is same as middle element, then return mid */ if (arr[mid] <= x && arr[mid+1] > x) return mid;
/* If x is greater than arr[mid], then either arr[mid + 1] is ceiling of x or ceiling lies in arr[mid+1...high] */ if(arr[mid] < x) return findCrossOver(arr, mid+1, high, x);
return findCrossOver(arr, low, mid - 1, x); }
// This function prints k closest elements to x in arr[]. // n is the number of elements in arr[] int printKclosest(int arr[], int x, int k, int n) { // Find the crossover point int l = findCrossOver(arr, 0, n-1, x); int r = l+1; // Right index to search int count = 0; // To keep track of count of elements // already printed int sum=0; //To hold the sum of k closest element
// If x is present in arr[], then reduce left index // Assumption: all elements in arr[] are distinct if (arr[l] == x) l--;
// Compare elements on left and right of crossover // point to find the k closest elements while (l >= 0 && r < n && count < k) { if (x - arr[l] < arr[r] - x){ sum+=arr[l]; System.out.print(arr[l--]+" "); } else { sum+=arr[r]; System.out.print(arr[r++]+" "); count++; } }
// If there are no more elements on right side, then // print left elements while (count < k && l >= 0) { sum+=arr[l]; System.out.print(arr[l--]+" "); count++; }
// If there are no more elements on left side, then // print right elements while (count < k && r < n) { sum+=arr[r]; System.out.print(arr[r++]+" "); count++; } //Return average of KClosest elements of an array for a given value of x return (sum/k); }
/* Driver program to check above functions */ public static void main(String args[]) { Kclosest ob = new Kclosest(); Random rand = new Random(); int index,x,avg; ArrayList
Collections.shuffle(list);
for(int n=10;n<=100;n=n*10) { long startTime = System.currentTimeMillis(); //Create array of size of n int []arr=new int[n];
//fill the array with random integers from 1 to 500 for(int i=0;i // First sort the array so that all occurrences become consecutive Arrays.sort(arr); //k varies from 3 to 10 for(int k=3;k<=10;k++) { long start = System.currentTimeMillis(); //50 runs of different values of x between 1 to 500 for(int j=1;j<=50;j++) { x=rand.nextInt(500) + 1; //calling printKclosest method avg=ob.printKclosest(arr, x, k, n); System.out.println(" The average of "+k+" KClosest elements of an array for a given value of x="+x+" is "+avg); int diff=java.lang.Math.abs(avg-x); System.out.println("overall average of the difference between the target X="+x+" values and the average of the"+k+"-closest values to X is "+diff+" "); } long end = System.currentTimeMillis(); long total = end - start; System.out.println(" Total Time execution for "+k+" Closest for array size "+n+" is " +total +" ms"); System.out.println(" "); } long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; System.out.println(" Total Time execution for "+n+" numbers is " +totalTime +" ms"); System.out.println(" "); } } } WHAT CODE I NEED TO PUT TO GET THE TOTAL AVERAGE OF overall average of the difference the target X values between the target AND the average of the K-closest values to the target X values OF ALL TOGETHER JAVA CODE
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