-
Notifications
You must be signed in to change notification settings - Fork 0
/
salary.cpp
71 lines (66 loc) · 1.15 KB
/
salary.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
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <algorithm>
using namespace std;
int count1 = 0;
bool all_same(int* arr, int n) {
if(n ==0 )
return true;
int val = arr[0];
for(int i=1; i<n;++i) {
if(arr[i] != val)
return false;
}
return true;
}
void equify(int* arr, int n) {
// sort(arr, arr+n); TLE n*n*log n
int max = arr[0];
int max_index = 0;
for(int i=1;i<n;++i) {
if(arr[i] > max){
max = arr[i];
max_index = i;
}
}
int min = arr[0];
int min_index = 0;
for(int i=1;i<n;++i) {
if(arr[i] < min) {
min = arr[i];
min_index = i;
}
}
int diff = max - min;
count1 += diff;
for(int i=0;i<n;++i) {
if(i != max_index) {
arr[i]+=diff;
}
}
}
void print_arr(int* arr, int n) {
for(int i=0;i<n;++i) {
cout << arr[i] << " ";
}
cout << "\n";
}
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;++i) {
cin >> arr[i];
}
count1 = 0;
bool same = all_same(arr, n);
while(!same) {
equify(arr, n);
// print_arr(arr, n);
same = all_same(arr, n);
}
cout << count1 << "\n";
}
}