/
RearrangeTheArray.cpp
62 lines (45 loc) · 1.13 KB
/
RearrangeTheArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
You are given an array/list NUM of integers.
You are supposed to rearrange the elements of
NUM such that no two adjacent elements will be the same
or find out if it not possible.
For example:
Input: arr[] = {1,1,1,2,2,2}
Output: {1,2,1,2,1,2}
//code
#include <bits/stdc++.h>
#include<queue>
#include<unordered_map>
class compare{
public:
bool operator()(pair<int,int>p1, pair<int,int> p2){
return p1.second < p2.second;
}
};
vector<int> rearrange(vector<int>& num)
{
vector<int> output;
priority_queue<pair<int,int> , vector<pair<int,int>>, compare> pq;
int n = num.size();
unordered_map<int,int> freq;
for(int i=0;i<n;i++){
freq[num[i]]++;
}
for(auto x: freq){
pq.push({x.first,x.second});
}
pair<int,int>prev{-1,-1};
while(pq.size()!=0){
pair<int,int> curr = pq.top();
pq.pop();
output.push_back(curr.first);
if(prev.second > 0){
pq.push(prev);
}
curr.second = curr.second-1;
prev = curr;
}
if(output.size() == num.size()){
return output;
}
return {-2147483648};
}