Find the nearest number

Report a typo

Write a program that finds the elements in an array of integers that are closest to a given integer. If you find several integers with the same distance to N, you should output all of them in ascending order. If there are several equal numbers, output them all.

Input: a set of integers and a number N.

Output: some number(s) from the given array.

Sample Input 1:

1 2 4 5
3

Sample Output 1:

2 4

Sample Input 2:

1 2 3 4
6

Sample Output 2:

4

Sample Input 3:

5 1 3 3 1 5
4

Sample Output 3:

3 3 5 5
Write a program in Java 17
import java.util.*;

public class Main {
public static void main(String[] args) {
// write your code here
}
}
___

Create a free account to access the full topic