Java || Detailed Explanation || Two Approaches
Approach -1 :\nFor n = 3\n If i = 0 , arr[0] = 2i + 1 = 1\n If i = 1 , arr[1] = 2i + 1 = 3\n If i = 2 , arr[2] = 2i + 1 = 5\nWe dec 5 by 1 and inc 1 by 1 \narr 
# Approach -1 :\n**For n = 3**\n* If i = 0 , arr[0] = 2i + 1 = 1\n* If i = 1 , arr[1] = 2i + 1 = 3\n* If i = 2 , arr[2] = 2i + 1 = 5\nWe dec 5 by 1 and inc 1 by 1 \narr : { 2 , 3 , 4 }\nWe dec 4 by 1 and inc 2 by 1\narr : { 3 , 3 , 3 }\nMin operation = 2\nAverage of all nos = ( 1 + 3 + 5 ) / 3 = 3\n\n**For  n = 4**\n* If i = 0 , arr[ 0 ] = 2i +1 = 1\n* If i = 1 , arr[ 1 ] = 2i + 1 = 3\n* If i = 2 , arr[ 2 ] = 2i + 1 = 5\n* If i = 3 , arr [ 3 ] = 2i + 1 = 7\nWe dec 7 by 1 & inc 1 by 1\narr : { 2 , 3, 5, 6 }\nWe dec 6 by 1 and inc 2 by 1\narr : { 3 , 3 , 5 , 5 }\nWe dec 5 by 1 and inc 3 by 1\narr : { 4 , 3, 4 , 5 }\nWe dec 5 by 1 and inc 3 by 1\narr : { 4 , 4, 4, 4 }\n\nmin operation = 4\nAverage of all elements = (1 + 3 + 5 + 7) / 4 = 4\n\n## Observation from the above examples : All Elements tend to acheive their average \n\narr :   {   1 ,   3  ,  5 ,  7 }\nops : { +3 , +1 , -1 , -3 }\nWe clearly see the pairs .\nWe have to inc arr[ 0 ] = 1 by 1,  3 times &  dec arr [ 3 ] = 7 , by 1, 3 times\nWe have to inc arr[ 1 ] = 1 by 1,  1 time &  dec arr [ 3 ] = 7 , by 1 , 3 times.\n\nSo , min operations = 3 + 1 = 4\n\n# Steps :\n* We find the average of all elements.\n* We create an array \'operations\' of size = n.\n* operations [ i ] = avg - arr [ i ] , i.e we calculate how much we have to add to make arr[ i ] =  avg. If arr[ i ] > avg , then , avg - arr [ i ] = -ve.\n* We add all non -ve  elements of operations array . That\'ll give us the final answer.\n# Time Complexity : O(n)\n# Code:\n```\nclass Solution {\n    public int minOperations(int n) \n    {\n        int arr[] = new int[n];\n        int sum = 0;\n        int operations[] = new int[n]; // operations[i] = stores deviation of arr[i] from average of arr elements\n        for(int i = 0 ; i<n ; i++)\n        {\n            arr[i] = (2*i)+1;\n            sum += arr[i];\n        }\n        int avg = sum/n;\n        int ans = 0;\n        for(int i = 0 ; i < n ; i++)\n        {\n            operations[i] = avg - arr[i];\n            if(operations[i]>=0)\n                ans += operations[i];\n        }\n        return ans;\n    }\n}\n```\n\n# Approach - 2:\n* As we know , for n = 4 \n   arr : { 1 , 3 , 5 , 7 }  &  avg = 4\n\t ops : { 3 , 1 , -1 , - 3 } \n\t min operations = 4\n\t We observe , that , our ans is sum of the AP series : { 1 , 3 } \n* For n = 3 ,\n  arr : { 1 , 3  , 5 } &  avg = 3\n\tops : { 2 , 0 , -2 }\n\tMin operations = 2\n\tWe observe , that , our ans is sum of the AP series : {  2 }\n\t\n* \tFor n = 5\n    arr : { 1 , 3 , 5 , 7 , 9 }  & avg = 5\n\t\tops : { 4 , 2 , 0 , -2 , -4 }\n\t\tmin operations = 6\n\t\tWe observe that our ans is sum of the AP series : {2 ,4 }\n\t\t\n*  For n = 6,\n    arr : { 1 ,3 , 5 , 7 , 9 , 11} & avg = 6\n\t\tops : { 5 , 3, 1 , -1 , -3 , -5 }\n\t\tmin operations = 9\n\t\tWe observe , our ans is the sum of the AP series : { 1 , 3 , 5}\n\t\t\n# \t\tFrom the above examples , we make a couple of conclusions:-\n **1.)When n = odd , The answer is sum of first (n/2) elements AP of series : { 2 , 4 ,6 ,8 ,....} **\n As we know ,  the sum of first n terms of an AP series = S = n/2[2a + (n \u2212 1) \xD7 d]\n Here a = 2 & d = 2 & n = n/2\n So, S =n/2[4 + ( n - 1 ) * 2 ]**\n          = n /2 [ 4 + 2n - 2 ]\n\t\t\t\t\t= n/2 [ 2 + 2n ] \n\t\t\t\t\t= n * (n+1)\nHere, n = n/ 2\nSo, S = n/2 * ( n/2 + 1)\n\t\t\t\t\t\n**2. ) When n = even , The answer is sum of first (n/2) elements of AP series : { 1 , 3 , 5 ,....}**\n As we know ,  the sum of first n terms of an AP series = S = n/2[2a + (n \u2212 1) \xD7 d]\n Here a = 1 & d = 2 & n = n/2\n So , S = n/2 [ 2 + 2(n-1)]\n           = n/2[ 2 + 2n - 2 ]\n\t\t\t\t\t = n/2 * 2n\n\t\t\t\t\t = n^2\nHere , n = n /2\nSo , S = (n/2)^2\n\n# Time Complexity : O(1)\n# Code:\n```\nclass Solution {\n    public int minOperations(int n) \n    {\n        int s = 0;\n        if(n%2 == 0)\n            s = (n/2) * (n/2);\n        else\n            s = (n/2) * (n/2 + 1);\n        return s;\n    }\n}\n```\n
['Java']