Skip to content

Commit

Permalink
Create 719. Find K-th Smallest Pair Distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Chayandas07 authored Aug 14, 2024
1 parent c61c5f7 commit 5e85842
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 719. Find K-th Smallest Pair Distance
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
int smallestDistancePair(vector<int>& nums, int k) {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int N=1e6+1;
int mp[N]={0};
sort(nums.begin(),nums.end());
int n=nums.size();
for (int i=0;i<n;i++){
for (int j=i+1;j<n;j++){
mp[abs(nums[i]-nums[j])]++;
}
}
int ct=0;
int diff=0;
for (int i=0;i<N;i++){
cout<<mp[i]<<endl;
ct+=mp[i];
if (ct>=k){
diff=i;
break;
}
}
return diff++;
}
};

0 comments on commit 5e85842

Please sign in to comment.