I don’t know about O(1) solution,

but i have a O(n) solution,

x, y, z should be distinct,

if you iterate for 1 to n, you would end up calculating 3^2 times the acutal answer.

##
why?

there would be 3 choices for every integer (x, y, z).

this problem is all about choices, if i ask you, to give me 2 distinct numbers that would add up to some n. how would you do?

i will do (n - 1) / 2

##
why?

assume n = 4,

there exists follwing pair of integers,

[(1,3),(2,2),(3,1)]

second pair doesn’t have unique values and first and last pair are same.

this pattern simply boils down to above equation.

now if we have 3 numbers we need to iterate first number from 1 to some values so that it wouldn’t calculate duplicate values.

for every first number (lets say i),

we need to find the number of valid pairs that would result in sum = (n - i) with values greater than i and they must be distinct.

more formally (i < j < k) (our triplet)

and in this way the last triplet would be ((n/3)-1,(n/3),(n/3)+1), which would result in sum = n,

if i get bigger than ((n - 1)/3) then j and k must be smaller than i (but we have already calulated the pairs with those values right?).

so i iterated i from 1 to (n / 3) - 1

and calulated all values.

##
My AC code

```
#include <iostream>
using namespace std;
main () {
int n;
cin >> n;
int ans = 0;
for (int i = 1; i < (n / 3); ++i) {
ans += ((n - i - 1) >> 1) - i;
}
cout << ans << '\n';
return 0;
}
```

English is not my first language, sorry for bad english.