[Codewars] Spinning Rings - Fidget Spinner Edition

Spinning Rings - Fidget Spinner Edition

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
typedef unsigned long long ull;

ull spinningRings(ull innerMax, ull outerMax) {
ull n = innerMax + 1, m = outerMax + 1;
if (m < n) {
ull r = n % m;
if (m % 2 == r % 2) return n - m + (m - r) / 2;
if (r % 2 == 0) return n - r / 2;
return 2 * n - (m + (2 * r + 1) % m - 1) / 2;
}
if (n % 2 == 0) return n / 2;
ull r = m % n;
if (r == 0) return m;
if (r % 2 == 1) return m + (n - r) / 2;
return m + n - r / 2;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/25/PS/Codewars/spinning-rings-fidget-spinner-edition/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.