[C++] simple solution with math technique | Time and Space: O(1)
I solved this problem by Mathematics technique.\nFirst, list the conditions at different n and we can observe that all the number converge to n :\nwhen n is odd
I solved this problem by Mathematics technique.\nFirst, list the conditions at different n and we can observe that all the number converge to n :\nwhen n is odd : \nn = 3: 1 3 5 \n-> 1 and 5 converge to 3 -> takes 2 operations\n\nn = 5: 1 3 5 7 9 \n-> 3 and 7 converge to 5 (2 op.), 1 and 9 converge to 5 as well (4 op.) -> take `2 + 4` operations\n\nn = 7: 1 3 5 7 9 11 13 \n-> take `2 + 4 + 6` operations\nBy formula of arithmetic series, we get that the result is `(n - 1) * (n + 1) / 4`\n\nLikewise, the case of "n is even" are as follows:\nn = 4 : 1 3 5 7 \n-> all the numbers converge to 4, 3 and 5 take 1 op. , 1 and 7 take 3 op. -> take `1 + 3` operation\n\nn = 6 : 1 3 5 7 9 11 \n-> take `1 + 3 + 5` operation\n\nBy formula of arithmetic series, we get that the result is `n * n / 4`\n```\nint minOperations(int n) {\n        if(n % 2)\n            return (n-1)*(n+1)/4;\n        else\n            return n * n / 4;\n    }\n```\n\nActually, we can further simplify it : \n```\n int minOperations(int n) {\n            return n * n / 4;\n    }\n```
[]