AlgoUniversity
  • Go Back
Discussion
K Closest :

Author

Gigaliths

Difficulty Level : Medium

Submissions : 1954

Asked In : Amazon

Marks :10

: 21 | : 9

Given an N-sized array of integers and value 'X', find the k closest integers with respect to 'X'.

It is guaranteed that X is present in the given array.

In case there are two values with same distance from 'X', prefer the smaller value.

Input

First line contains a single integer corresponding to integer N.

Second line contains N space separated integers corresponding to the values in the given array.

Third line contains two space separated integers corresponding to 'k' and 'X'

Constraints

$$$ 1 \le k < N \le 100000 $$$

$$$ 1 \le arr[i] , X \le 10^9 $$$

Output

Print K-closest integers in sorted order. (Ascending Order)

Example

Input
10
1 2 3 4 5 10 9 8 7 6
3 4
Output
2 3 5 

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.