1
+ /*
2
+ -> Problem Statement
3
+ You are given an array of points having x and y co-ordinates (vector of vectors) and an integer k,
4
+ You need to return k closest points to origin according to euclidean distance.
5
+
6
+ -> Approach Explanation:
7
+ - First for all points (vector having x and y) , we will map value of x^2 + y^2 with its key as point's index.
8
+ ( since we will be using int while comparing distance we are not taking square root of distance,
9
+ since taking square root can not make comparision exact for int , and not taking sqrt will not affect while comparing
10
+ - We need K closest points i.e. K points having shortest distance which can be found using priority_queue as minheap. )
11
+ - Priority queue is considered as vector of vectors (each having distance and index taken from map),
12
+ priority_queue will keep vectors in assending order of distance as per given comparision function.
13
+ And we can get first k closest points from index inside first k priority_queue vectors.
14
+ */
15
+
16
+ #include < iostream>
17
+ #include < map>
18
+ #include < queue>
19
+ using namespace std ;
20
+
21
+ class Solution {
22
+ public:
23
+
24
+ bool operator ()(vector<int > const & a, vector<int > const & b ) // comparision function to sort according to distance
25
+ {
26
+ return a[0 ] > b[0 ];
27
+ }
28
+
29
+ vector<vector<int >> kClosest (vector<vector<int >>& points, int k) {
30
+
31
+ map<int ,int >mp;
32
+ for (int i=0 ;i<points.size ();i++)
33
+ {
34
+ mp[i] = (points[i][0 ] * points[i][0 ]) + (points[i][1 ] * points[i][1 ]); // mapping distances and index for points
35
+ }
36
+
37
+ priority_queue<vector<int >, vector<vector<int >>, Solution> pq; // priority_queue as minheap(for distances)
38
+
39
+ for (auto i : mp)
40
+ {
41
+ vector<int >v;
42
+ v.push_back (i.second );
43
+ v.push_back (i.first );
44
+ pq.push (v); // pushing distances and indexes from maps to priority_queue
45
+ }
46
+
47
+ vector<vector<int >>ans;
48
+
49
+ while (k--)
50
+ {
51
+ vector<int >top = pq.top (); // getting first k vectors having shortest distances
52
+ vector<int >xy = points[top[1 ]]; // getting corrsponding points from index
53
+ ans.push_back (xy);
54
+ pq.pop ();
55
+ }
56
+
57
+ return ans; // returning final vector having k closest points from origin
58
+
59
+ }
60
+ };
61
+
62
+ // It can also be easily solved with similar logic just by using vector of vectors or map having distances and indices in sorted manner.
63
+
64
+ int main ()
65
+ {
66
+ cout<<" \n Enter number of test cases: " ;
67
+ int t; cin>>t; // no. of test cases
68
+
69
+ /*
70
+ Constraints:
71
+ 1 <= k <= points array length <= 10^4
72
+ -10^4 < x,y values < 10^4
73
+ */
74
+
75
+ while (t--)
76
+ {
77
+ cout<<" \n\n Enter number of points you have: " ;
78
+ int n; cin>>n;
79
+
80
+ vector<vector<int >> points (n);
81
+
82
+ cout<<" \n Enter points as x and y co-ordinates in array : " ;
83
+ for (int i=0 ; i<n; i++)
84
+ {
85
+ cout<<" \n Point :" <<i + 1 <<endl;
86
+ cout<<" \n x :" ;
87
+ cin>>points[i][0 ];
88
+ cout<<" \n y :" ;
89
+ cin>>points[i][1 ];
90
+ }
91
+
92
+ cout<<" \n Enter value of K (No of Closest points you want): " ;
93
+ int k;cin>>k;
94
+
95
+ Solution *ob;
96
+ cout<<" \n K Closest Poits to Origin are :" ;
97
+ vector<vector<int >> closestpoints = ob-> kClosest (points,k);
98
+ for (int i=0 ; i<k; i++)
99
+ {
100
+ cout<<" \n Point :" <<i + 1 <<endl;
101
+ cout<<" \n x :" ;
102
+ cin>>points[i][0 ];
103
+ cout<<" \n y :" ;
104
+ cin>>points[i][1 ];
105
+ }
106
+
107
+ }
108
+ return 0 ;
109
+ }
110
+
111
+ /*
112
+ SAMPLE INPUT
113
+ 2 // number of test cases
114
+ 3 // no.of points for test case:1
115
+ 4 3 // points as x and y in vector for test case:1
116
+ -1 2
117
+ -5 3
118
+ 2 // value of k
119
+ 5 // no.of pointsfor test case:2
120
+ -2 5 // points as x and y in vector for test case:2
121
+ 0 0
122
+ 1 -6
123
+ 7 -4
124
+ 3 // value of k
125
+
126
+ SAMPLE OUTPUT (excluding interactive instructions)
127
+ -1 2 ( 2 clost points to origin for test case-1 )
128
+ 4 3
129
+ 0 0 ( 3 closest points to origin for test case-2 )
130
+ -2 5
131
+ 1 -6
132
+ COMPLEXITY ANALYSIS : N= no.of points in array
133
+ Time : O(N) to iterate array/vector and insert in map .
134
+ O(NLogN) for creating minheap or inserting elements in priority_que
135
+ Hence, O(NlogN) overall.
136
+ Space: O(N) for inputs and aux space: O(N) to create a priority_queue.
137
+ Hence, O(N) overall.
138
+ */
0 commit comments