[Hacker Rank] Sansa and XOR

Sansa and XOR

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
int sansaXor(vector<int> A) {
long long res = 0, r = A.size(), l = 1;
for(auto& a : A) {
auto c = l * r;
if(c & 1) res ^= a;
r--,l++;
}
return res;
}

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