forked from durgesh2001/hacktoberfest_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.cpp
More file actions
17 lines (16 loc) · 810 Bytes
/
priorityqueue.cpp
File metadata and controls
17 lines (16 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Firstly we make a min heap and store all the elements of array with their absolute value as a key and array element itself as the value of key.Then the value at top is the closest number to zero.
//if there is are multiple same keys in heap then we search the availability of its greater value in array if it is not present then we print second value otherwise first.
class Solution {
public:
int findClosestNumber(vector<int>& nums) {
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
for(int i=0;i<nums.size();i++)
{
pq.push({abs(nums[i]),nums[i]});
}
if(pq.top().second<0 && find(nums.begin(),nums.end(),pq.top().first)==nums.end())
return pq.top().second;
else
return pq.top().first;
}
};