Advertisement
Kulas_Code20

Number_One

Jun 30th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package projects;
  2.  
  3. import java.util.Scanner;
  4. public class NumberOne {
  5.  
  6.     // Returns element closest to target in arr[]
  7.     public static int findClosest(int arr[], int target) {
  8.         int n = arr.length;
  9.  
  10.         // Corner cases
  11.         if (target <= arr[0])
  12.             return arr[0];
  13.         if (target >= arr[n - 1])
  14.             return arr[n - 1];
  15.  
  16.         // Doing binary search
  17.         int i = 0, j = n, mid = 0;
  18.         while (i < j) {
  19.             mid = (i + j) / 2;
  20.  
  21.             if (arr[mid] == target)
  22.                 return arr[mid];
  23.  
  24.             /*
  25.              * If target is less than array element, then search in left
  26.              */
  27.             if (target < arr[mid]) {
  28.  
  29.                 // If target is greater than previous
  30.                 // to mid, return closest of two
  31.                 if (mid > 0 && target > arr[mid - 1])
  32.                     return getClosest(arr[mid - 1], arr[mid], target);
  33.  
  34.                 /* Repeat for left half */
  35.                 j = mid;
  36.             }
  37.  
  38.             // If target is greater than mid
  39.             else {
  40.                 if (mid < n - 1 && target < arr[mid + 1])
  41.                     return getClosest(arr[mid], arr[mid + 1], target);
  42.                 i = mid + 1; // update i
  43.             }
  44.         }
  45.  
  46.         // Only single element left after search
  47.         return arr[mid];
  48.     }
  49.  
  50.     // Method to compare which one is the more close
  51.     // We find the closest by taking the difference
  52.     // between the target and both values. It assumes
  53.     // that val2 is greater than val1 and target lies
  54.     // between these two.
  55.     public static int getClosest(int val1, int val2, int target) {
  56.         if (target - val1 >= val2 - target)
  57.             return val2;
  58.         else
  59.             return val1;
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.         Scanner sc = new Scanner(System.in);
  64.         int sum = 0;
  65.         int avg = 0;
  66.         int[] arr = new int[10];
  67.  
  68.         // Get input from the user
  69.         for (int i = 0; i < 10; i++) {
  70.             System.out.print("Enter number[" + (i + 1) + "]: ");
  71.             arr[i] = sc.nextInt();
  72.             sum += arr[i];
  73.             avg = sum / 10;
  74.         }
  75.         System.out.println("The average: " + avg);
  76.  
  77.         // Print the numbet that is greater than average.
  78.         System.out.println("\nNumber that is greater than average: ");
  79.         for (int j = 0; j < 10; j++) {
  80.             if (arr[j] > avg) {
  81.                 System.out.print(arr[j] + " ");
  82.             }
  83.         }
  84.         // Print the closest value to the average.
  85.         System.out.println("\nThe value that closest to average: \n" + findClosest(arr, avg));
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement