[C++]✅ 1 Line O(1) Solution || 2 Approaches
Intuition\n Describe your first thoughts on how to solve this problem. \n- For odd n, transform every array element to the middle element in the array.\n- For e
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- For odd **n**, transform every array element to the middle element in the array.\n- For even **n**, transform every array element to the everage of two middle elements in the array.\n- Why this works? for every element in the first half of array that gets increamented by **x**, there is a corresponding element in the second half that needs to be decreamented by **x** for it to be equal to the final value.\n\n---\n\n\n\n# Code\n```\nclass Solution {\npublic:\n    int minOperations(int n) {\n        int ans=0;\n        for(int i=1;i<=n;i+=2)\n            ans += (n - i);\n        return ans;\n    }\n};\n//TC : O(n)\n```\n\n- From this we can easily derive a mathematical formulae\n- We are adding n (n/2) times\n- From this subtract the sum of odd number uptill n.\n- [Sum of all odd numbers uptill n](https://www.cuemath.com/algebra/sum-of-odd-numbers/) = pow((n/2),2) = (n/2)*(n/2)\n- So, ans = n*(n/2) - (n/2)*(n/2)\n- ans = (n*n)/4\n\n# Code\n```\nclass Solution {\npublic:\n    int minOperations(int n) {\n        return (n*n)/4;\n    }\n};\n//TC : O(1)\n```\n\n\n\n\n\n
['Array', 'Greedy', 'C++']