 GeeksforGeeks App
Open App Browser
Continue

# Find k closest elements to a given value

Given a sorted array arr[] and a value X, find the k closest elements to X in arr[].

Examples:

```Input: K = 4, X = 35
arr[] = {12, 16, 22, 30, 35, 39, 42,
45, 48, 50, 53, 55, 56}
Output: 30 39 42 45```

Note that if the element is present in array, then it should not be in output, only the other closest elements are required.

Recommended Practice

In the following solutions, it is assumed that all elements of array are distinct.

A simple solution is to do linear search for k closest elements.

1. Start from the first element and search for the crossover point (The point before which elements are smaller than or equal to X and after which elements are greater). This step takes O(n) time.
2. Once we find the crossover point, we can compare elements on both sides of crossover point to print k closest elements. This step takes O(k) time.

The time complexity of the above solution is O(n).

An Optimized Solution is to find k elements in O(Logn + k) time. The idea is to use Binary Search to find the crossover point. Once we find index of crossover point, we can print k closest elements in O(k) time.

## C++

 `#include` `/* Function to find the cross over point (the point before``which elements are smaller than or equal to x and after``which greater than x)*/``int` `findCrossOver(``int` `arr[], ``int` `low, ``int` `high, ``int` `x)``{``// Base cases``if` `(arr[high] <= x) ``// x is greater than all``    ``return` `high;``if` `(arr[low] > x) ``// x is smaller than all``    ``return` `low;` `// Find the middle point``int` `mid = (low + high)/2; ``/* low + (high - low)/2 */` `/* If x is same as middle element, then return mid */``if` `(arr[mid] <= x && arr[mid+1] > x)``    ``return` `mid;` `/* If x is greater than arr[mid], then either arr[mid + 1]``    ``is ceiling of x or ceiling lies in arr[mid+1...high] */``if``(arr[mid] < x)``    ``return` `findCrossOver(arr, mid+1, high, x);` `return` `findCrossOver(arr, low, mid - 1, x);``}` `// This function prints k closest elements to x in arr[].``// n is the number of elements in arr[]``void` `printKclosest(``int` `arr[], ``int` `x, ``int` `k, ``int` `n)``{``    ``// Find the crossover point``    ``int` `l = findCrossOver(arr, 0, n-1, x);``    ``int` `r = l+1; ``// Right index to search``    ``int` `count = 0; ``// To keep track of count of elements already printed` `    ``// If x is present in arr[], then reduce left index``    ``// Assumption: all elements in arr[] are distinct``    ``if` `(arr[l] == x) l--;` `    ``// Compare elements on left and right of crossover``    ``// point to find the k closest elements``    ``while` `(l >= 0 && r < n && count < k)``    ``{``        ``if` `(x - arr[l] < arr[r] - x)``            ``printf``(``"%d "``, arr[l--]);``        ``else``            ``printf``(``"%d "``, arr[r++]);``        ``count++;``    ``}` `    ``// If there are no more elements on right side, then``    ``// print left elements``    ``while` `(count < k && l >= 0)``        ``printf``(``"%d "``, arr[l--]), count++;` `    ``// If there are no more elements on left side, then``    ``// print right elements``    ``while` `(count < k && r < n)``        ``printf``(``"%d "``, arr[r++]), count++;``}` `/* Driver program to check above functions */``int` `main()``{``int` `arr[] ={12, 16, 22, 30, 35, 39, 42,``            ``45, 48, 50, 53, 55, 56};``int` `n = ``sizeof``(arr)/``sizeof``(arr);``int` `x = 35, k = 4;``printKclosest(arr, x, 4, n);``return` `0;``}`

## Java

 `// Java program to find k closest elements to a given value``class` `KClosest``{``    ``/* Function to find the cross over point (the point before``       ``which elements are smaller than or equal to x and after``       ``which greater than x)*/``    ``int` `findCrossOver(``int` `arr[], ``int` `low, ``int` `high, ``int` `x)``    ``{``        ``// Base cases``        ``if` `(arr[high] <= x) ``// x is greater than all``            ``return` `high;``        ``if` `(arr[low] > x)  ``// x is smaller than all``            ``return` `low;` `        ``// Find the middle point``        ``int` `mid = (low + high)/``2``;  ``/* low + (high - low)/2 */` `        ``/* If x is same as middle element, then return mid */``        ``if` `(arr[mid] <= x && arr[mid+``1``] > x)``            ``return` `mid;` `        ``/* If x is greater than arr[mid], then either arr[mid + 1]``          ``is ceiling of x or ceiling lies in arr[mid+1...high] */``        ``if``(arr[mid] < x)``            ``return` `findCrossOver(arr, mid+``1``, high, x);` `        ``return` `findCrossOver(arr, low, mid - ``1``, x);``    ``}` `    ``// This function prints k closest elements to x in arr[].``    ``// n is the number of elements in arr[]``    ``void` `printKclosest(``int` `arr[], ``int` `x, ``int` `k, ``int` `n)``    ``{``        ``// Find the crossover point``        ``int` `l = findCrossOver(arr, ``0``, n-``1``, x);``        ``int` `r = l+``1``;   ``// Right index to search``        ``int` `count = ``0``; ``// To keep track of count of elements``                       ``// already printed` `        ``// If x is present in arr[], then reduce left index``        ``// Assumption: all elements in arr[] are distinct``        ``if` `(arr[l] == x) l--;` `        ``// Compare elements on left and right of crossover``        ``// point to find the k closest elements``        ``while` `(l >= ``0` `&& r < n && count < k)``        ``{``            ``if` `(x - arr[l] < arr[r] - x)``                ``System.out.print(arr[l--]+``" "``);``            ``else``                ``System.out.print(arr[r++]+``" "``);``            ``count++;``        ``}` `        ``// If there are no more elements on right side, then``        ``// print left elements``        ``while` `(count < k && l >= ``0``)``        ``{``            ``System.out.print(arr[l--]+``" "``);``            ``count++;``        ``}`  `        ``// If there are no more elements on left side, then``        ``// print right elements``        ``while` `(count < k && r < n)``        ``{``            ``System.out.print(arr[r++]+``" "``);``            ``count++;``        ``}``    ``}` `    ``/* Driver program to check above functions */``    ``public` `static` `void` `main(String args[])``    ``{``        ``KClosest ob = ``new` `KClosest();``        ``int` `arr[] = {``12``, ``16``, ``22``, ``30``, ``35``, ``39``, ``42``,``                     ``45``, ``48``, ``50``, ``53``, ``55``, ``56``                    ``};``        ``int` `n = arr.length;``        ``int` `x = ``35``, k = ``4``;``        ``ob.printKclosest(arr, x, ``4``, n);``    ``}``}``/* This code is contributed by Rajat Mishra */`

## Python3

 `# Function to find the cross over point``# (the point before which elements are``# smaller than or equal to x and after``# which greater than x)``def` `findCrossOver(arr, low, high, x) :` `    ``# Base cases``    ``if` `(arr[high] <``=` `x) : ``# x is greater than all``        ``return` `high``        ` `    ``if` `(arr[low] > x) : ``# x is smaller than all``        ``return` `low``    ` `    ``# Find the middle point``    ``mid ``=` `(low ``+` `high) ``/``/` `2` `# low + (high - low)// 2``    ` `    ``# If x is same as middle element,``    ``# then return mid``    ``if` `(arr[mid] <``=` `x ``and` `arr[mid ``+` `1``] > x) :``        ``return` `mid``    ` `    ``# If x is greater than arr[mid], then``    ``# either arr[mid + 1] is ceiling of x``    ``# or ceiling lies in arr[mid+1...high]``    ``if``(arr[mid] < x) :``        ``return` `findCrossOver(arr, mid ``+` `1``, high, x)``    ` `    ``return` `findCrossOver(arr, low, mid ``-` `1``, x)` `# This function prints k closest elements to x``# in arr[]. n is the number of elements in arr[]``def` `printKclosest(arr, x, k, n) :``    ` `    ``# Find the crossover point``    ``l ``=` `findCrossOver(arr, ``0``, n ``-` `1``, x)``    ``r ``=` `l ``+` `1` `# Right index to search``    ``count ``=` `0` `# To keep track of count of``              ``# elements already printed` `    ``# If x is present in arr[], then reduce``    ``# left index. Assumption: all elements``    ``# in arr[] are distinct``    ``if` `(arr[l] ``=``=` `x) :``        ``l ``-``=` `1` `    ``# Compare elements on left and right of crossover``    ``# point to find the k closest elements``    ``while` `(l >``=` `0` `and` `r < n ``and` `count < k) :``        ` `        ``if` `(x ``-` `arr[l] < arr[r] ``-` `x) :``            ``print``(arr[l], end ``=` `" "``)``            ``l ``-``=` `1``        ``else` `:``            ``print``(arr[r], end ``=` `" "``)``            ``r ``+``=` `1``        ``count ``+``=` `1` `    ``# If there are no more elements on right``    ``# side, then print left elements``    ``while` `(count < k ``and` `l >``=` `0``) :``        ``print``(arr[l], end ``=` `" "``)``        ``l ``-``=` `1``        ``count ``+``=` `1` `    ``# If there are no more elements on left``    ``# side, then print right elements``    ``while` `(count < k ``and` `r < n) :``        ``print``(arr[r], end ``=` `" "``)``        ``r ``+``=` `1``        ``count ``+``=` `1` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=``[``12``, ``16``, ``22``, ``30``, ``35``, ``39``, ``42``,``              ``45``, ``48``, ``50``, ``53``, ``55``, ``56``]``                ` `    ``n ``=` `len``(arr)``    ``x ``=` `35``    ``k ``=` `4``    ` `    ``printKclosest(arr, x, ``4``, n)``    ` `# This code is contributed by Ryuga`

## C#

 `// C# program to find k closest elements to``// a given value``using` `System;` `class` `GFG {``    ` `    ``/* Function to find the cross over point``    ``(the point before which elements are``    ``smaller than or equal to x and after which``    ``greater than x)*/``    ``static` `int` `findCrossOver(``int` `[]arr, ``int` `low,``                                ``int` `high, ``int` `x)``    ``{``        ` `        ``// Base cases``        ``// x is greater than all``        ``if` `(arr[high] <= x)``            ``return` `high;``            ` `        ``// x is smaller than all``        ``if` `(arr[low] > x)``            ``return` `low;` `        ``// Find the middle point``        ``/* low + (high - low)/2 */``        ``int` `mid = (low + high)/2;` `        ``/* If x is same as middle element, then``        ``return mid */``        ``if` `(arr[mid] <= x && arr[mid+1] > x)``            ``return` `mid;` `        ``/* If x is greater than arr[mid], then``        ``either arr[mid + 1] is ceiling of x or``        ``ceiling lies in arr[mid+1...high] */``        ``if``(arr[mid] < x)``            ``return` `findCrossOver(arr, mid+1,``                                      ``high, x);` `        ``return` `findCrossOver(arr, low, mid - 1, x);``    ``}` `    ``// This function prints k closest elements``    ``// to x in arr[]. n is the number of``    ``// elements in arr[]``    ``static` `void` `printKclosest(``int` `[]arr, ``int` `x,``                                  ``int` `k, ``int` `n)``    ``{``        ` `        ``// Find the crossover point``        ``int` `l = findCrossOver(arr, 0, n-1, x);``        ` `        ``// Right index to search``        ``int` `r = l + 1;``        ` `        ``// To keep track of count of elements``        ``int` `count = 0;` `        ``// If x is present in arr[], then reduce``        ``// left index Assumption: all elements in``        ``// arr[] are distinct``        ``if` `(arr[l] == x) l--;` `        ``// Compare elements on left and right of``        ``// crossover point to find the k closest``        ``// elements``        ``while` `(l >= 0 && r < n && count < k)``        ``{``            ``if` `(x - arr[l] < arr[r] - x)``                ``Console.Write(arr[l--]+``" "``);``            ``else``                ``Console.Write(arr[r++]+``" "``);``            ``count++;``        ``}` `        ``// If there are no more elements on right``        ``// side, then print left elements``        ``while` `(count < k && l >= 0)``        ``{``            ``Console.Write(arr[l--]+``" "``);``            ``count++;``        ``}` `        ``// If there are no more elements on left``        ``// side, then print right elements``        ``while` `(count < k && r < n)``        ``{``            ``Console.Write(arr[r++] + ``" "``);``            ``count++;``        ``}``    ``}` `    ``/* Driver program to check above functions */``    ``public` `static` `void` `Main()``    ``{``        ``int` `[]arr = {12, 16, 22, 30, 35, 39, 42,``                         ``45, 48, 50, 53, 55, 56};``        ``int` `n = arr.Length;``        ``int` `x = 35;``        ``printKclosest(arr, x, 4, n);``    ``}``}` `// This code is contributed by nitin mittal.`

## PHP

 ` ``\$x``)``        ``return` `\$low``;``    ` `    ``// Find the middle point``    ``/* low + (high - low)/2 */``    ``\$mid` `= (``\$low` `+ ``\$high``)/2;``    ` `    ``/* If x is same as middle``       ``element, then return mid */``    ``if` `(``\$arr``[``\$mid``] <= ``\$x` `and``        ``\$arr``[``\$mid` `+ 1] > ``\$x``)``        ``return` `\$mid``;``    ` `    ``/* If x is greater than arr[mid],``       ``then either arr[mid + 1] is``       ``ceiling of x or ceiling lies``       ``in arr[mid+1...high] */``    ``if``(``\$arr``[``\$mid``] < ``\$x``)``        ``return` `findCrossOver(``\$arr``, ``\$mid` `+ 1,``                                 ``\$high``, ``\$x``);``    ` `    ``return` `findCrossOver(``\$arr``, ``\$low``,``                      ``\$mid` `- 1, ``\$x``);``}` `// This function prints k``// closest elements to x in arr[].``// n is the number of elements``// in arr[]``function` `printKclosest(``\$arr``, ``\$x``, ``\$k``, ``\$n``)``{``    ` `    ``// Find the crossover point``    ``\$l` `= findCrossOver(``\$arr``, 0, ``\$n` `- 1, ``\$x``);``    ` `    ``// Right index to search``    ``\$r` `= ``\$l` `+ 1;``    ` `    ``// To keep track of count of``    ``// elements already printed``    ``\$count` `= 0;` `    ``// If x is present in arr[],``    ``// then reduce left index``    ``// Assumption: all elements``    ``// in arr[] are distinct``    ``if` `(``\$arr``[``\$l``] == ``\$x``) ``\$l``--;` `    ``// Compare elements on left``    ``// and right of crossover``    ``// point to find the k``    ``// closest elements``    ``while` `(``\$l` `>= 0 ``and` `\$r` `< ``\$n``              ``and` `\$count` `< ``\$k``)``    ``{``        ``if` `(``\$x` `- ``\$arr``[``\$l``] < ``\$arr``[``\$r``] - ``\$x``)``            ``echo` `\$arr``[``\$l``--],``" "``;``        ``else``            ``echo` `\$arr``[``\$r``++],``" "``;``        ``\$count``++;``    ``}` `    ``// If there are no more``    ``// elements on right side,``    ``// then print left elements``    ``while` `(``\$count` `< ``\$k` `and` `\$l` `>= 0)``        ``echo` `\$arr``[``\$l``--],``" "``; ``\$count``++;` `    ``// If there are no more``    ``// elements on left side,``    ``// then print right elements``    ``while` `(``\$count` `< ``\$k` `and` `\$r` `< ``\$n``)``        ``echo` `\$arr``[``\$r``++]; ``\$count``++;``}` `// Driver Code``\$arr` `=``array``(12, 16, 22, 30, 35, 39, 42,``                ``45, 48, 50, 53, 55, 56);``\$n` `= ``count``(``\$arr``);``\$x` `= 35; ``\$k` `= 4;` `printKclosest(``\$arr``, ``\$x``, 4, ``\$n``);` `// This code is contributed by anuj_67.``?>`

## Javascript

 ``

Output

`39 30 42 45 `

Time complexity: O(Logn + k).
Auxiliary Space: O(1), since no extra space has been used.

Approach 2: Using Priority Queue

This approach uses a priority queue (max heap) to maintain the k closest numbers to x. It iterates over the elements in the array and calculates their absolute differences from x. The pairs of absolute differences and negative values are pushed into the max heap. If the size of the max heap exceeds k, the element with the maximum absolute difference is removed. Finally, the top k elements from the max heap are extracted and stored in a result vector. The vector is then reversed to obtain the closest numbers in ascending order before being returned as the result.

Below is the implementation:

## C++

 `#include ``using` `namespace` `std;` `vector<``int``> findClosestElements(vector<``int``>& arr, ``int` `k,``                                ``int` `x)``{``    ``// Create a max heap to store the pairs of absolute``    ``// differences and negative values``    ``priority_queue > maxH;``  ` `      ``int` `n = arr.size();` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``// Skip if the element is equal to x``        ``if` `(arr[i] == x)``            ``continue``;` `        ``// Calculate the absolute difference and add the``        ``// pair to the max heap``        ``maxH.push({ ``abs``(arr[i] - x), -arr[i] });` `        ``// If the size of the max heap exceeds k, remove the``        ``// element with the maximum absolute difference``        ``if` `(maxH.size() > k)``            ``maxH.pop();``    ``}` `    ``// Store the result in a vector``    ``vector<``int``> result;` `    ``// Retrieve the top k elements from the max heap``    ``while` `(!maxH.empty()) {``        ``// Get the top element from the max heap``        ``auto` `p = maxH.top();``        ``maxH.pop();` `        ``// Add the negative value to the result vector``        ``result.push_back(-p.second);``    ``}` `    ``// Reverse the result vector to get the closest numbers``    ``// in ascending order``    ``reverse(result.begin(), result.end());` `    ``return` `result;``}` `int` `main()``{``    ``vector<``int``> arr = { 12, 16, 22, 30, 35, 39, 42,``                        ``45, 48, 50, 53, 55, 56 };``    ``int` `k = 4, x = 35;``    ``vector<``int``> res = findClosestElements(arr, k, x);``    ``for` `(``int` `i = 0; i < res.size(); i++) {``        ``cout << res[i] << ``" "``;``    ``}``    ``cout << endl;``    ``return` `0;``}`

Output

```39 30 42 45
```

Time Complexity: O(n log k), where n is the size of the array and k is the number of elements to be returned. The priority queue takes O(log k) time to insert an element and O(log k) time to remove the top element. Therefore, traversing through the array and inserting elements into the priority queue takes O(n log k) time. Popping elements from the priority queue and pushing them into the result vector takes O(k log k) time. Therefore, the total time complexity is O(n log k + k log k) which is equivalent to O(n log k).
Auxiliary Space: O(k), as we are using a priority queue of size k+1 and a vector of size k to store the result.

Exercise: Extend the optimized solution to work for duplicates also, i.e., to work for arrays where elements don’t have to be distinct.

My Personal Notes arrow_drop_up