C Program to Find K Closest Median Elements

This is a C Program to find k numbers cloest to median of S, S being a set of n numbers.

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

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5.  
  6. int N = 25;
  7. int sequence[25];
  8.  
  9. void sort() {
  10.     int i, j, temp;
  11.     for (i = 1; i < N; i++) {
  12.         j = i;
  13.         temp = sequence[i];
  14.         while (j > 0 && temp < sequence[j - 1]) {
  15.             sequence[j] = sequence[j - 1];
  16.             j = j - 1;
  17.         }
  18.         sequence[j] = temp;
  19.     }
  20. }
  21.  
  22. int getMedian() {
  23.     if (N % 2 == 0)
  24.         return ((sequence[N / 2 - 1] + sequence[N / 2]) / 2);
  25.     else
  26.         return sequence[N / 2];
  27. }
  28.  
  29. int main(int argc, char **argv) {
  30.     int i, j;
  31.     for (i = 0; i < N; i++)
  32.         sequence[i] = rand() % (100 - 1 + 1) + 1;
  33.     sort();
  34.     printf("The Sequence is: ");
  35.     for (i = 0; i < N; i++)
  36.         printf("%d ", sequence[i]);
  37.  
  38.     int median = getMedian();
  39.     printf("\nEnter the number of elements close to median you want: ");
  40.  
  41.     int k;
  42.     scanf("%d", &k);
  43.  
  44.     if (N % 2 == 0) {
  45.         i = N / 2 - 1;
  46.         j = N / 2;
  47.     } else {
  48.         i = N / 2 - 1;
  49.         j = N / 2 + 1;
  50.     }
  51.     int flag = 0;
  52.     int n;
  53.     for (n = 0; n < k; n++) {
  54.         if (median - sequence[i] < sequence[j] - median) {
  55.             printf("%d ", sequence[i]);
  56.             i--;
  57.             if (i == -1) {
  58.                 n++;
  59.                 flag = 1;
  60.                 break;
  61.             }
  62.         } else {
  63.             printf("%d ", sequence[j]);
  64.             j++;
  65.             if (j == N) {
  66.                 n++;
  67.                 break;
  68.             }
  69.         }
  70.     }
  71.     while (n < k) {
  72.         if (flag == 1) {
  73.             printf("%d ", sequence[j]);
  74.             j++;
  75.             n++;
  76.         } else {
  77.             printf("%d ", sequence[i]);
  78.             i--;
  79.             n++;
  80.         }
  81.     }
  82. }

Output:

$ gcc KCloseToMedian.c
$ ./a.out
 
The Sequence is: 1 3 5 6 25 28 28 35 37 42 43 46 54 59 62 63 65 68 70 79 82 92 92 93 96 
Enter the number of elements close to median you want: 2
59 62

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C 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.