-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathnumbers_smaller_than_current.cpp
More file actions
93 lines (88 loc) · 1.69 KB
/
numbers_smaller_than_current.cpp
File metadata and controls
93 lines (88 loc) · 1.69 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
void print(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int *method1(int *arr, int n)
{
int *ans = new int[n];
for (int i = 0; i < n; i++)
{
int cnt = 0;
for (int j = 0; j < n; j++)
{
if (j != i && arr[j] < arr[i])
{
cnt++;
}
}
ans[i] = cnt;
}
return ans;
}
int *method2(int *arr, int n)
{
int *temp = new int[n];
for (int i = 0; i < n; i++)
{
temp[i] = arr[i];
}
sort(temp, temp + n);
map<int, int> m;
for (int i = 0; i < n; i++)
{
if (m.find(temp[i]) == m.end())
{
m[temp[i]] = i;
}
}
int *ans = new int[n];
for (int i = 0; i < n; i++)
{
ans[i] = m[arr[i]];
}
return ans;
}
int *method3(int *arr, int n)
{
int *temp = new int[100];
for (int i = 0; i < 100; i++)
{
temp[i] = 0;
}
for (int i = 0; i < n; i++)
{
temp[arr[i]]++;
}
for (int i = 0; i < 100; i++)
{
if (i != 0)
{
temp[i] = temp[i - 1] + temp[i];
}
}
int *ans = new int[n];
for (int i = 0; i < n; i++)
{
ans[i] = temp[arr[i] - 1];
}
return ans;
}
int main()
{
int arr[] = {8, 1, 2, 2, 3};
int n = sizeof(arr) / sizeof(int);
print(arr, n);
// int *ans = method1(arr, n);
// int *ans = method2(arr, n);
int *ans = method3(arr, n);
print(ans, n);
return 0;
}