Skip to content

Commit b13d179

Browse files
authored
Merge pull request #1321 from SuyashSingh01/main
Math -K-closest-cpp
2 parents fb5fc9d + 9877065 commit b13d179

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

Math/K_closest.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Author: SUYASH SINGH
3+
4+
Problem statement:-Find K Closest Elements
5+
Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.
6+
7+
An integer a is closer to x than an integer b if:
8+
9+
|a - x| < |b - x|, or
10+
|a - x| == |b - x| and a < b
11+
Explaination:
12+
13+
Input: points = [[1,3],[-2,2]], k = 1
14+
Output: [[-2,2]]
15+
Explanation:
16+
The distance between (1, 3) and the origin is sqrt(10).
17+
The distance between (-2, 2) and the origin is sqrt(8).
18+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
19+
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
20+
Example 2:
21+
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
22+
Output: [[3,3],[-2,4]]
23+
Explanation: The answer [[-2,4],[3,3]] would also be accepted.
24+
*/
25+
26+
#include <iostream>
27+
#include <vector>
28+
#include <queue>
29+
#include <cmath>
30+
31+
using namespace std;
32+
33+
class Solution {
34+
public:
35+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
36+
// Answer vector
37+
vector<vector<int>> result(k);
38+
// Max heap storage initialization
39+
priority_queue<vector<int>> maxHeap;
40+
// Construction of max heap
41+
for (auto& p : points) {
42+
int x = p[0], y = p[1];
43+
// Calculate the squared distance from the origin using the formula x^2 + y^2
44+
// Store the distance along with the coordinates (x, y) in the maxHeap
45+
maxHeap.push({x * x + y * y, x, y});
46+
// If the size of the maxHeap exceeds k, remove the point with the maximum distance
47+
if (maxHeap.size() > k) {
48+
maxHeap.pop();
49+
}
50+
}
51+
// Extract the k closest points from the maxHeap and store them in the result vector
52+
for (int i = k - 1; i >= 0; --i) {
53+
vector<int> top = maxHeap.top();
54+
maxHeap.pop();
55+
result[i] = {top[1], top[2]};
56+
}
57+
58+
// Return the result vector containing the k closest points
59+
60+
return result;
61+
}
62+
};
63+
64+
int main() {
65+
vector<vector<int>> points = {{1, 3}, {-2, 2}, {5, -1}, {0, 0}, {3, 4}};
66+
int k = 3;
67+
68+
Solution solution;
69+
vector<vector<int>> closestPoints = solution.kClosest(points, k);
70+
71+
cout << "The " << k << " closest points to the origin are:\n";
72+
for (const auto& point : closestPoints) {
73+
cout << "(" << point[0] << ", " << point[1] << ")\n";
74+
}
75+
76+
return 0;
77+
}/*
78+
Time Complexity:
79+
80+
Constructing the maxHeap: the overall time complexity is O(N log K).
81+
Extracting the k closest points: O(K log K). We extract the top element from the maxHeap k times, each operation taking O(log K) time. Hence, the time complexity is O(K log K).
82+
Therefore, the overall time complexity of the solution is O(N log K), where N is the number of points and K is the value of k.
83+
84+
Space Complexity:
85+
86+
The maxHeap stores at most k elements, so the space complexity for the maxHeap is O(K).
87+
The result vector stores k closest points, resulting in O(K) space.
88+
Apart from these, the solution uses a constant amount of space for variables and temporary storage.*/

kclosest_sol.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Author : SUYASH SINGH
2+
/*
3+
Input: points = [[1,3],[-2,2]], k = 1
4+
Output: [[-2,2]]
5+
6+
Explanation:
7+
8+
The distance between (1, 3) and the origin is sqrt(10).
9+
The distance between (-2, 2) and the origin is sqrt(8).
10+
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
11+
We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]].
12+
*/
13+
#include <iostream>
14+
#include <vector>
15+
16+
using namespace std;
17+
18+
class Solution {
19+
public:
20+
vector<vector<int>> kClosest(vector<vector<int>>& points, int k) {
21+
vector<pair<double, vector<int>>> v;
22+
for(auto x: points) {
23+
double dis = sqrt(x[0] * x[0] + x[1] * x[1]);
24+
v.push_back({dis, {x[0], x[1]}});
25+
}
26+
sort(v.begin(), v.end());
27+
vector<vector<int>> ans;
28+
for(int i=0; i<k; i++) ans.push_back(v[i].second);
29+
return ans;
30+
}
31+
};
32+
33+
int main() {
34+
// create a vector of points
35+
vector<vector<int>> points {{3, 4}, {1, 2}, {-1, -1}, {0, 0}, {2, 3}, {-2, 2}};
36+
int k = 3;
37+
Solution s;
38+
vector<vector<int>> closest_points = s.kClosest(points, k);
39+
cout << "The " << k << " closest points to the origin are:\n";
40+
for(auto point: closest_points) {
41+
cout << "(" << point[0] << ", " << point[1] << ")\n";
42+
}
43+
return 0;
44+
}

0 commit comments

Comments
 (0)