C++ | easy explanation | 100% fast | Short Code
In this question we have array where A[i] = 2i + 1. \n\n For making elements equal we try to reduce all elements to a central number as it is closest to element
In this question we have array where A[i] = 2i + 1. \n\n* For making elements equal we try to reduce all elements to a central number as it is closest to elements on both sides of it.\n* If number of elements are odd we have a central element and we try to reduce elements on its sides to that number. {1,3,5,7,9} (In this for reducing 3 and 7 we need 2 operations as we can reduce just 1 in one operation so in 2 operations we get {1,5,5,5,9} similary for 1 and 9 we need 4 operations and so on). So we will need 2 + 4 + ....... n/2 operations.\n* In even case we will reduce to a central number but this time operations will start from 1 and next 3 and so on because {1,3,5,7} has center number 4 and it needs 1 operation to make 3,4,5 equal then it will need 3 operation. Total operation will be 1+3+5 + .... n/2 operations.\n* We need n/2 operations because we update both sides in a single operation.\n```\nclass Solution {\npublic:\n    \n    int helper(int start, int diff,int elements){\n        int ans = 0;\n        for(int i = 0 ; i< elements ; i++)\n        {\n            ans = ans + start;\n            start = start + diff;\n        }\n        return ans;\n    }\n    \n    int minOperations(int n) {\n        //if n is odd then we have a center number and try to reduce other numbers to center number with number of operations starting from 2 and next being 4 and 6 and so on.\n        if(n % 2 != 0){\n            return helper(2,2,n/2);\n        } // if n is even then we try to reduce numbers of a number that is mid of two middle elements with number of operations starting from 1 and next 3 and 5 and so on.\n        else{\n            return helper(1,2,n/2);\n        }\n    }\n};\n```
['C']