C++ code with explanation O(1) approach and O(n) approach
Intuition\nIntuition: the array is composed of n odd elements and no matter how much operation we perform the sum of all elements will be same because if we inc
# Intuition\nIntuition: the array is composed of n odd elements and no matter how much operation we perform the sum of all elements will be same because if we increase one element then we also decrease the other.\n\n# Approach\nThus using Arthemetic Progesssion to determine the sum of n odd numbers which is n^2.\n then we distribute n^2 through the array of size n that means n^2/n which is n. \nThus for an ideal array all element will be equal to n\n  Eg:    --  arr[size:6] =1 3 5 7 9 11   sum(n^2)=6*6=36\n    ---- ideal array is :   6 6 6 6 6 6\n     ----    differnce is:  -5 -3 -1 1 3 5\n   \nNow all we have to do is count how many units of +ve and -ve \nwill collide to yield ideal array. In above example ans is 9 (5 and -5 \ncollide thus 5 operation,  3 and -3 thus 3 opn, 1 and -1 thus 1 opn, and now adding them all 5+3+1=9  return 9)\n\nNote this is for even of elements for odd no center element will be 0 eg:\n      1 3 5 7 9\n      5 5 5 5 5 \n      -4 -2 0 2 4   ---   ans is 4+2 = 6\n\n# Complexity\n- Time complexity -- O(n)\n\n- Space complexity -- O(1)\n\n# Code-1\n```\nclass Solution {\npublic:\n\n    int minOperations(int n) {\n        int ans=0;\n        for(int i=0;i<n/2;i++){  \n            //Traversing half the array because rest half is \n            // redundant info coz of palindrome\n            ans+=(n-((2*i)+1));\n        }\n         return ans;\n    }\n};\n\n\n```\n# Code -2 \n```\nclass Solution {\npublic:\n    int minOperations(int n) {\n        \n         //This code also has the same approah but instead of loop i have used AProgression formula\n\n\n         if(n%2==0)return n*n/4; //Sum of n/2 odd number\n         \n         return (n/2)*((n/2)+1); //Sum of n/2 even number\n        \n\n    }\n};\n```\n
['C']