
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
This is a C++ Program to find k numbers closest to Median of S, where S is a set of n numbers.
Algorithms
Begin function partition() for partitioning the array on the basis of values at high as pivot value: Arguments: a[]=an array. l=low H=high Body of the function: Declare variables pivot, in, i Initialize in = l Set pivot = h For i=l to h-1 if(a[i] < a[pivot]) swap a[i] and a[in]) increment in. swap a[pivot] and a[in] return in. End Begin function QuickSort() to implement quicksort algorithm to sort the data elements: Arguments: a[]=an array. l=low h=high Body of the function: declare pindex if(l < h) index = Partition(a, l, h) QuickSort(a, l, pindex-1); QuickSort(a, pindex+1, h); Return 0. End Begin Function main(), If the number of the data element are odd, Assign the middle index to low and the index next to it to high and calculate median. Run a loop for k times and print the element which his closer to the median. Else The median will be an average of two middle values . Run a loop for k times and print the element which his closer to the median. End
Example
#include<iostream> using namespace std; void swap(int *x, int *y) { //swapping two values int tmp; tmp = *x; *x = *y; *y = tmp; } int Partition(int a[], int l, int h) { int pivot, in, i; in = l; pivot = h; for(i=l; i < h; i++) { if(a[i] < a[pivot]) { swap(&a[i], &a[in]); in++; } } swap(&a[pivot], &a[in]); return in; } int QuickSort(int a[], int l, int h) { int pindex; if(l < h) { pindex = Partition(a, l, h); QuickSort(a, l, pindex-1); QuickSort(a, pindex+1, h); } return 0; } int main() { int n, i, h, l, k; double d1,d2, median; cout<<"Enter the number of element in dataset: "; cin>>n; int a[n]; for(i = 0; i < n; i++) { cout<<"\nEnter "<<i+1<<" element: "; cin>>a[i]; } cout<<"\nEnter the number of element nearest to the median required: "; cin>>k; QuickSort(a, 0, n-1); cout<<"The K element nearest to the median are: "; if(n%2 == 1) { median = a[n/2]; h = n/2+1; l= n/2; while(k > 0) { if((median-a[l] <= a[h]-median) && l >= 0) { cout<<" "<<a[l]; l--; k--; } else if((median-a[l] > a[h]-median) && h <= n-1) { cout<<" "<<a[h]; h++; k--; } } } else { d1 = a[n/2]; d2 = a[n/2-1]; median = (d1+d2)/2; h = n/2; l = n/2-1; while(k > 0) { d1 = a[l]; d2 = a[h]; if((median-d2 <= d1-median) && l >= 0) { cout<<" "<<a[l]; l--; k--; } else if((median-d2 > d1-median) && h <= n-1) { cout<<" "<<a[h]; h++; k--; } } } return 0; }
Output
Enter the number of element in dataset: 7 Enter 1 element: 7 Enter 2 element: 6 Enter 3 element: 5 Enter 4 element: 4 Enter 5 element: 3 Enter 6 element: 2 Enter 7 element: 1 Enter the number of element nearest to the median required: 2 The K element nearest to the median are: 4 3
- Related Articles
- Find all combinations of k-bit numbers with n bits set where 1
- C++ program to find maximum possible median of elements whose sum is s
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Design a DFA machine accepting odd numbers of 0’s or even numbers of 1’s
- 8086 program to find average of n numbers
- Find k closest numbers in an unsorted array in C++
- Program to find number of values factors of two set of numbers
- Java program to find the sum of n natural numbers
- Java Program to Find Sum of N Numbers Using Recursion
- Haskell Program to Find Sum of N Numbers Using Recursion
- C# Program to Count the Number of 1's in the Entered Numbers
- Find N distinct numbers whose bitwise Or is equal to K in Python
- Find N distinct numbers whose bitwise Or is equal to K in C++
- 8085 program to find the sum of first n natural numbers
- C++ Program to Find the GCD and LCM of n Numbers

Advertisements