Pure Mathematics 0ms Runtime solution Easy Solution and Explained
Approach\nThe problem requires only AP of even and odd numbers basically so Let\'s use some examples and then we can see a pattern\n\n -> For n = even \n   n = 
# Approach\nThe problem requires only AP of even and odd numbers basically so Let\'s use some examples and then we can see a pattern\n\n -> For n = even \n   n = 6\narray = [1,3,5,7,9,11] \nSum of the elements = 36 (why?? sum of  n odd integers is n^2)\n```\nMid of the array will be = 6\n\n Now, to make element equal to 6\n\n                 [1     3    5    6    7    9     11]\noperations =     +5    +3   +1        -1   -3     -5\n\nTotal operation required to make every element 6 = 1 + 3 + 5 = 9\n\n\n```\n\nn = 8\narray = [1,3,5,7,9,11,13,15] \nSum of the elements = 64 (why?? sum of  n odd integers is n^2)\n```\nMid of the array will be = 8\n\n Now, to make element equal to 8\n\n                 [1    3    5    7    8    9    11    13    15]\noperations =     +7   +5   +3   +1        -1    -3    -5    -7\n\nTotal operation required to make every element 8 = 1 + 3 + 5 + 7 = 16\n\n```\n\n```\nwe can clearly see a pattern over here which is "Sum of odd elements till n/2" \nwhich is = " n/2 * n/2 "\n\n\n```\n\n\n-> For n = odd\nn = 5\narray = [1,3,5,7,9] \nSum of the elements = 25 (why?? sum of  n odd integers is n^2)\n```\nMid of the array will be = 5\n\n Now, to make element equal to 5\n\n                 [1    3    5    7    9]\noperations =     +4   +2    0   -2   -4  \n\nTotal operation required to make every element 5 = 2 + 4 = 6\n2*(1 + 2), twice the sum of first 2 elements\n\n\n```\n\nn = 7\narray = [1,3,5,7,9,11,13] \nSum of the elements = 49 (why?? sum of  n odd integers is n^2)\n```\nMid of the array will be = 7\n\n Now, to make element equal to 7\n\n                 [1    3    5    7    9    11    13]\noperations =     +6   +4   +2    0   -2    -4    +6\n\nTotal operation required to make every element 7 = 2 + 4 + 6 = 12\n or we can say 2*(1 + 2 + 3), twice the sum of first 3 elements\n\n\n```\n\n```\nwe can clearly see a pattern over here which is "Twice of Sum of natural elements till (n-1)/2"\n\n Calculations:\n        If (n-1)/2 = m\n        -> 2 * Sum of (m)/2\n        -> 2 * (m * (m+1) )/2\n        -> m * (m+1)\n        -> (n-1)/2) * ((n-1)/2) + 1 \n\n\n```\n\n\n# Complexity\n- Time complexity: $$O(1)$$\n\n- Space complexity: $$O(1)$$ \n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minOperations(int n) {\n        if(n%2==0)\n            return (n/2)*(n/2);\n        else{\n            return ((n-1)/2) * (((n-1)/2) +1 );\n        }\n    }\n};\n```\n\n\n```\nIf you like the Explanation an upvote will be appreciated and if there is any \nerror please comment down \uD83D\uDE0A\uD83D\uDE0A\n\n\n```
['Math', 'C++']