✅ Simple O(1) 1-Liner | Easy Solution w/ Explanation | Beats 100%
Solution (Make Equal to Mid Element)\n\nThe main thing in this problem is that we must realize that to make array equal in minimum operations, we need to make a
***Solution (Make Equal to Mid Element)***\n\nThe main thing in this problem is that we must realize that to make array equal in minimum operations, we need to **make all the elements equal to the Mid element** (which would be equal to `n`). Also, if we make the first half equal to the mid element, the other half will also become equal since one operation consists of adding and subtracting. There will always be a pair of elements which need same number of addition and subtraction to become equal to mid.\n\nFor this, there exists two cases. You may understand it better if you consider an example alongside -\n\n* **n is Odd** : In this case, all the elements need to become equal to the mid element( equal to `n`). This is done by adding 1 to elements smaller than mid and subtracting 1 from elements bigger than mid till all become equal.\n\n\tThe **total number of operations** needed are - `2 + 4 + 6 + 8 + ... (n - 1) /2 elements`. How `(n - 1) / 2` elements ? These correspond to the number to elements coming before mid. So, we need to apply operations to all those elements (2 times, 4 times, 6 times and so on...).\n\t\n\tThe given equation is in *Arithmetic Progression*, which can be simplified to - \n\t```\n\t=> N * [a + ((N - 1) * d) / 2]      // A.P Formula\n\t=  N * [2 + ((N -1)) * 2) / 2]\n\t=  N * [2 + N - 1]\n\t=  N * [N + 1]\n\twhere N = number of elements in the Arithmetic Progression = (n -1) / 2 \n\t```\n\n* **n is Even** - In this case, all the elements need to become equal to the mean of mid elements (equal to `n`). Again, the operations are applied as in the case when n is odd by adding to smaller than mid & subtracting from larger than mid elements.\n\n\tThe **total number of operations** needed are - `1 + 3 + 5 + 7 + ... n /2 elements`. How `n / 2` elements ? These again correspond to the number to elements coming before the mean of mid elements. In even case, the first half and second half contains n/2 elements each. So, we need to apply operations to all those elements (1 times, 3 times, 5 times and so on...).\n\t\n\tThe given equation is in *Arithmetic Progression*, which can be simplified to - \n\t```\n\t=> N * [a + ((N - 1) * d) / 2]      // A.P Formula\n\t=  N * [1 + ((N - 1) * 2) / 2]\n\t=  N * [1 + N - 1]\n\t=  N * N\n\twhere N = number of elements in the Arithmetic Progression = n / 2 \n\t```\n\t\n----\t\n\t\nThe implementation of above idea is given below - \n\n```\nint minOperations(int n) {\n\tif(n & 1){ // Odd Case\n\t\tint N = (n - 1) / 2;  // number of elements before mid element\n\t\treturn N * (N + 1); \n\t}\n\tint N = n / 2;  // number of elements before mean of mid elements\n\treturn N * N; \n}\n```\n\n***Time Complexity :*** **`O(1)`**\n***Space Complexity :*** **`O(1)`**\n\n----\n\n***Solution - II (1 - Liner)***\n\nThe below is the same solution as above just modified into 1-Liner. Just apply simple math to simplify the above equations -\n\n```\nint minOperations(int n) {\n\treturn (n & 1 ? n * --n : n * n) >> 2;    // x >> 2 = x / 4\n}\n```\n\n*More Simplfied -*\n\n```\nint minOperations(int n) {\n\treturn n * n >> 2; // or n*n/4\n}\n```\n\n![image](https://assets.leetcode.com/users/images/1fe0195b-2da0-41f3-95ea-66f9b6e8cec6_1617696798.1046119.png)\n
['Math', 'C']