K Closest Numbers
Medium

Problem Statement

Given a sorted array, a number X, and a value k, find the k closest numbers to X in the array.

Examples

1Example 1
Input:
{ "arr": [ 5, 6, 7, 8, 9 ], "k": 3, "x": 7 }
Output:
[ 6, 7, 8 ]
2Example 2
Input:
{ "arr": [ 1, 2, 3, 4, 5 ], "k": 4, "x": 3 }
Output:
[ 1, 2, 3, 4 ]
3Example 3
Input:
{ "arr": [ 10, 20, 30, 40, 50 ], "k": 2, "x": 35 }
Output:
[ 30, 40 ]
Loading...

Sign in to Run Code and Submit