
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find k closest elements to a given value in C++
Consider we have an array A with few elements. We have two other value X and k. Our task is to find the k number of nearest elements of X from the array A. If the element X is present in the array, then it will not be shown in the output. If A = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56] and X = 35, k = 4. The output will be 30, 39, 42, 45.
To solve this, we will use the binary search approach. Using this we will get the crossover point. If the index of the crossover point has found, we can print k-closest elements in O(k) time.
Example
#include<iostream> using namespace std; int getCrossoverPoint(int arr[], int left, int right, int x) { if (arr[right] <= x) return right; if (arr[left] > x) return left; int mid = (left + right)/2; if(arr[mid] <= x && arr[mid+1] > x) return mid; if(arr[mid] < x) return getCrossoverPoint(arr, mid+1, right, x); return getCrossoverPoint(arr, left, mid - 1, x); } void findKClosestNumbers(int arr[], int x, int k, int n) { int l = getCrossoverPoint(arr, 0, n-1, x); int r = l+1; int count = 0; if (arr[l] == x) l--; while (l >= 0 && r < n && count < k) { if (x - arr[l] < arr[r] - x) cout << arr[l--] << " "; else cout << arr[r++] << " "; count++; } while (count < k && l >= 0){ cout << arr[l--] << " "; count++; } while (count < k && r < n){ cout << arr[r++] << " "; count++; } } int main() { int arr[] ={12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56}; int n = sizeof(arr)/sizeof(arr[0]); int x = 35, k = 5; findKClosestNumbers(arr, x, k, n); }
Output
39 30 42 45 48
- Related Articles
- Find K Closest Elements in C++
- Find the closest index to given value in JavaScript
- Find three closest elements from given three sorted arrays in C++
- Program to find three unique elements from list whose sum is closest to k Python
- Find K Closest Points to the Origin in C++
- Program to find k where k elements have value at least k in Python
- Find number from given list for which value of the function is closest to A in C++
- Find k closest numbers in an unsorted array in C++
- Program to find k where given matrix has k by k square of same value in C++
- How to find the value closest to positive infinity in Python?
- How to find the value closest to negative infinity in Python?
- Find the k smallest numbers after deleting given elements in C++
- Find the closest value of an array in JavaScript
- Find the k largest numbers after deleting the given elements in C++
- Find closest value for every element in array in C++

Advertisements