Skip to content

Add C++ solution for 0658-find-k-closest-elements #4277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions cpp/0658-find-k-closest-elements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Approach: Binary Search + Two‑Pointer Window Expansion (More Intuitive)
// Runtime: 0 ms (100%); Memory: 35.7 MB (99%)
// Time Complexity: O(log(n) + k), Space Complexity: O(1)

class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {

// Part 1: Binary Sarch to find x or element in arr closest to x
int l=0;
int r = arr.size()-1;
int closestVal = arr[0];
int closestIndex = 0;

while(l<=r){
int m = (l+r)/2;
int currDiff = abs(arr[m] - x);
int resDiff = abs(closestVal - x);

if((currDiff< resDiff) || (currDiff == resDiff && arr[m]<closestVal)){
closestVal = arr[m];
closestIndex = m;
}

if (arr[m]<x){
l=m+1;
}
else if(arr[m]>x){
r= m-1;
}
else{
break;
}

}

//Part 2: Binary Search to find the window of k such elements
l=closestIndex;
r=closestIndex;

for( int i=0; i<k-1;i++){
if(l==0){
r+=1;
}
else if(r== arr.size()-1 ){
l-=1;
}
else if (x - arr[l - 1] <= arr[r + 1] - x){
l-=1;
}
else{
r+=1;

}


}
return vector<int>(arr.begin() + l, arr.begin() + r + 1);

}
};