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.
//This is a java program to find k numbers closest to median of N numbers
import java.util.Random;
import java.util.Scanner;
public class K_Close_Numbers_Median
{
static int N = 25;
static int[] sequence = new int[N];
public static void sort()
{
int i, j, temp;
for (i = 1; i < N; i++)
{
j = i;
temp = sequence[i];
while (j > 0 && temp < sequence[j - 1])
{
sequence[j] = sequence[j - 1];
j = j - 1;
}
sequence[j] = temp;
}
}
public static int median()
{
if(N%2 == 0)
return ((sequence[N/2-1] + sequence[N/2])/2);
else
return sequence[N/2];
}
public static void main(String args[])
{
Random random = new Random();
for(int i=0; i<N; i++)
sequence[i] = Math.abs(random.nextInt(100));
sort();
System.out.println("The Sequence is: ");
for(int i=0; i<N; i++)
System.out.print(sequence[i] + " ");
int median = median();
System.out.println("\nEnter the number of elements close to median you want: ");
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int i, j;
if(N%2 == 0)
{
i = N/2-1;
j = N/2;
}
else
{
i = N/2-1;
j = N/2+1;
}
boolean flag = false; int n;
for(n=0; n<k; n++)
{
if(median-sequence[i] < sequence[j]-median)
{
System.out.print(sequence[i] + " ");
i--;
if(i == -1)
{
n++;
flag = true;
break;
}
}
else
{
System.out.print(sequence[j] + " ");
j++;
if(j == N)
{
n++;
break;
}
}
}
while(n < k)
{
if(flag == true)
{
System.out.print(sequence[j] + " ");
j++;
n++;
}
else
{
System.out.print(sequence[i] + " ");
i--;
n++;
}
}
}
}
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
Related posts:
Java Program to Implement Skew Heap
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement Regular Falsi Algorithm
The Difference Between Collection.stream().forEach() and Collection.forEach()
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Spring Boot - CORS Support
Compact Strings in Java 9
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Từ khóa static và final trong java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Checked and Unchecked Exceptions in Java
Java Program to Perform Searching Based on Locality of Reference
Java Program to Implement ConcurrentLinkedQueue API
Spring MVC + Thymeleaf 3.0: New Features
Spring Boot Security Auto-Configuration
Introduction to Spring Cloud CLI
Spring Boot - Creating Docker Image
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Hash Tables Chaining with Binary Trees
Spring Security OAuth2 – Simple Token Revocation
Spring Boot Change Context Path
Getting the Size of an Iterable in Java
Custom Thread Pools In Java 8 Parallel Streams
Spring Boot - Batch Service
Java Program to Implement Binary Heap
Hướng dẫn Java Design Pattern – Adapter
A Guide to Spring Cloud Netflix – Hystrix
How to Convert List to Map in Java
Quick Guide to the Java StringTokenizer
Generate Spring Boot REST Client with Swagger
Câu lệnh điều khiển vòng lặp trong Java (break, continue)