[Hacker Rank] Equal

Equal

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
long long equal(vector<int> arr) {
vector<long long> req(5);
long long mi = *min_element(begin(arr), end(arr));
// make minimum - 0 to minimum - 4 and get minimin operations
for(int i = 0; i < 5; i++) {
for(auto& a : arr) {
long long d = a - mi;
long long five = 0, two = 0, one = 0;
five = d / 5; d -= 5 * five;
two = d / 2; d -= 2 * two;
one = d;
req[i] += five + two + one;
}
mi--;
}
return *min_element(begin(req), end(req));
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/12/PS/HackerRank/equal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.