Java Program to Find K Closest Median Elements

This is a java program to find K such elements given by users, such that those numbers are closer to the median of the given sequence. We first find the median of the sequence and then compare with each element such that the difference between the median and number is minimum, and print such k elements.

Here is the source code of the Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find k numbers closest to median of N numbers
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class K_Close_Numbers_Median 
  6. {
  7.     static int N = 25;
  8.     static int[] sequence = new int[N];
  9.  
  10.     public static void sort() 
  11.     {
  12.         int i, j, temp;
  13.         for (i = 1; i < N; i++) 
  14.         {
  15.             j = i;
  16.             temp = sequence[i];
  17.             while (j > 0 && temp < sequence[j - 1]) 
  18.             {
  19.                 sequence[j] = sequence[j - 1];
  20.                 j = j - 1;
  21.             }
  22.             sequence[j] = temp;
  23.         }
  24.     }
  25.  
  26.     public static int median()
  27.     {
  28.         if(N%2 == 0)
  29.             return ((sequence[N/2-1] + sequence[N/2])/2);
  30.         else
  31.             return sequence[N/2];
  32.     }
  33.     public static void main(String args[])
  34.     {
  35.         Random random = new Random();
  36.  
  37.         for(int i=0; i<N; i++)
  38.             sequence[i] = Math.abs(random.nextInt(100));
  39.         sort();
  40.         System.out.println("The Sequence is: ");
  41.         for(int i=0; i<N; i++)
  42.             System.out.print(sequence[i] + " ");
  43.  
  44.         int median = median();
  45.         System.out.println("\nEnter the number of elements close to median you want: ");
  46.         Scanner sc = new Scanner(System.in);
  47.         int k = sc.nextInt();
  48.         int i, j;
  49.         if(N%2 == 0)
  50.         {
  51.             i = N/2-1;
  52.             j = N/2;
  53.         }
  54.         else
  55.         {
  56.             i = N/2-1;
  57.             j = N/2+1;
  58.         }
  59.         boolean flag = false; int n;
  60.         for(n=0; n<k; n++)
  61.         {
  62.             if(median-sequence[i] < sequence[j]-median)
  63.             {
  64.                 System.out.print(sequence[i] + " ");
  65.                 i--;
  66.                 if(i == -1)
  67.                 {
  68.                     n++;
  69.                     flag = true;
  70.                     break;
  71.                 }
  72.             }
  73.             else 
  74.             {
  75.                 System.out.print(sequence[j] + " ");
  76.                 j++;
  77.                 if(j == N)
  78.                 {
  79.                     n++;
  80.                     break;
  81.                 }
  82.             }
  83.         }
  84.         while(n < k)
  85.         {
  86.             if(flag == true)
  87.             {
  88.                 System.out.print(sequence[j] + " ");
  89.                 j++;
  90.                 n++;
  91.             }
  92.             else
  93.             {
  94.                 System.out.print(sequence[i] + " ");
  95.                 i--;
  96.                 n++;
  97.             }
  98.         }
  99.     }
  100. }

Output:

$ javac K_Close_Number_Median.java
$ java K_Close_Number_Median
 
The Sequence is: 
3 6 14 17 21 27 27 35 35 38 38 40 40 41 41 43 55 67 73 77 79 82 82 83 87 
Enter the number of elements close to median you want: 
5
40 41 41 38 38

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.