-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count_Smaller_elements.cpp
41 lines (38 loc) · 1021 Bytes
/
Count_Smaller_elements.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
class Solution {
public:
int binarySearch(vector<int> &temp, int key){
int index = 1;
int low = 0;
int high = temp.size()-1;
while(low<=high){
int mid = (low+high)/2;
if(temp[mid] == key){
index = mid;
high = mid-1;
}
else if(temp[mid]>key){
high = mid-1;
}
else{
low = mid+1;
}
}
return index;
}
vector<int> constructLowerArray(vector<int> &arr) {
vector<int> res(arr.size());
vector<int> temp;
for(int i = 0; i<arr.size(); i++){
temp.push_back(arr[i]);
}
sort(temp.begin(), temp.end());
for(int i=0; i<arr.size(); i++){
int index = binarySearch(temp, arr[i]);
res[i] = index;
vector<int>::iterator it;
it = temp.begin()+index;
temp.erase(it);
}
return res;
}
};