JAVA Simple Mathematics
EXPLANATION AND IDEA:\nCase 1: when n is odd\nLet say n is 5 \n\narr = [1 ,3 , 5 , 7, 9]\n\nIn one operation we can +1 arr[x] and -1 arr[y] where x<y . okay so 
**EXPLANATION AND IDEA:**\nCase 1: when n is odd\nLet say n is 5 \n```\narr = [1 ,3 , 5 , 7, 9]\n```\nIn one operation we can +1 arr[x] and -1 arr[y] where x<y . okay so best way to operate is fix mid that mean change every value to arr[mid]=5  ,  do operation on neighbours. i.e x=mid-1 , y=mid+1\n```\n           arr = [1    , 3   , 5  ,  7   , 9 ]\n                  +4    +2      0    -2   -4\nafter     arr  = [5   ,  5   , 5  ,  5  ,  5  ] \t\t\t\t  \n```\nTotal operation :\n=4+ 2 \n=2(2+1)\n=2(1+2) where 2 is n/2 let say mid=n/2\n=2(sum(mid))  sum: 1 + 2 + 3 + .. n/2 (sum(x) : sum of x natural number)\n=2 ( mid * (mid + 1) ) /2\n=mid *( mid+1 )\n\nCASE 2: when n is even \nlet say n=4\n```\narr = [ 1 , 3 , 5 , 7]\n```\nIdea is same match the number around mid. but here dont have proper mid so change this mid i.e 3 to 4 and 5 to 4 by performing one operation and now change every value of array to 4.\n```\n  arr= [ 1  , 3  , 5  , 7  ]\n         +3  +1   -1   -3\n\tarr =[4 ,  4 ,  4 ,  4]\t\t\n```\t\t\t\nTotal operation:\n=1+3   (where 3 is n/2+1)\n=1 + 3 + 5 + 7 + ... (mid+1) [ aritmatic progression with a=1 , d =2 , total terms = n/2 i.e mid] \n(Sum = n/2[2a + (n \u2212 1) \xD7 d])\n=mid * mid \n     \n**CODE:** \n```\n   public int minOperations(int n) {\n        int mid=n/2;\n        return n%2==0?mid*mid:mid*(mid+1);\n    }\n```\n\nAnother way to code:\n```\npublic int minOperations(int n) {\n        int mid=n/2;\n        return mid*(mid+n%2);\n    }\n```\n\n**Complexity:**\n`Time:O(1) and Space:O(1)`\n\nPlease **upvote**  if found it helpful and feel free to reach out to me or comment down if you have any doubt.\n
['Math', 'Java']