Java solution - O(1) space and time complexities - With explanation
The array is always going to be a list of odd numbers. Let\'s say, n = 4\n\nIn this case, the array will be [1, 3, 5, 7]. Note that this is an arithmetic progre
The array is always going to be a list of odd numbers. Let\'s say, n = 4\n\nIn this case, the array will be [1, 3, 5, 7]. Note that this is an arithmetic progression and therefore the average (in this case, 4) will lie in the middle of the array. So we can solve this problem by making all elements equal to the average.\n\nTo make all the elements 4, we need to add the following: [+3, +1, -1, -3]. Let\'s call this array `addArray`. This corresponds to 4 operations.\n\nLet\'s take another case where the array has odd number  of elements: n = 5. In this case, the array will be [1, 3, 5, 7, 9]. The average is 5. To make all the elements 5, we need to add the following: [+4, +2, +0, -2, -4]. This corresponds to 6 operations.\n\nObserve that if we can simply add the positive numbers in `addArray`, we get the required result. Since the positive numbers form an arithmetic progression, we can use the formula for finding the sum of the elements of an arithmetic progression: S = num/2 ( 2 * a + (num - 1) * d) where S is the sum, num is the number of elements in the progression, a is the first element and d is the difference between each pair of elements.\n\nSince the number of positive numbers in `addArray` is half the number of elements in the original array, we have\n\n**when n is odd:**\nnum = n/2\na = 2\nd = 2\n\nS = n/2 + (n^2)/4\n\n**when n is even:**\nnum = n/2\na = 1\nd = 2\n\nS = n^2\n\n```\nclass Solution {\n    public int minOperations(int n) {\n        int num = n / 2;\n        if (n % 2 == 0) {\n            return (num * (2 * 1 + (num - 1) * 2)) / 2;\n        } else {\n            return (num * (2 * 2 + (num - 1) * 2)) / 2;\n        }\n    }\n}\n```\n\nWith simplified formulae:\n\n```\nclass Solution {\n    public int minOperations(int n) {\n        int num = n / 2;\n        if (n % 2 == 0) {\n            return (int) Math.pow(n/2, 2);\n        } else {\n            return (int) (n/2 + Math.pow(n/2, 2));\n        }\n    }\n}\n```
['Java']