[JAVA] 1 liner solution with detailed explanation, beats 100%
The solution is based on the insight that the sum of all elements in the array is not changed based on operations becase we are subtracting 1 and adding 1 in ev
The solution is based on the insight that the sum of all elements in the array is not changed based on operations becase we are subtracting 1 and adding 1 in every operaiton. Based on that fact and the requirement that all elements in the array are equal we can learn more about what should be the end goal.\n\nIf we look at the sum for every n:\n\n```\nn=1 => 1\nn=2 => 1+3=4\nn=3 => 1+3+5=9\nn=4 => 1+3+5+7=16\n```\n\nWe can see that` sum(n) = n^2`. If all elements are equal, then every element has to be: `sum(n) / n` which solves to `(n^2)/n = n`.\n\nEvery operation will increase the leftmost value and decrease the rightmost value until they are both equal to `n`. That means we have in total `n/2` operations (rounded down) where the amount of operations for every `i` until `n/2` is `n-arr[i]`.\nWe are getting the arithmetic sequence of the amount of operations:\n```\na1 = n-1\nd = 2\n```\nThe element at `n/2` will be `1` when `n` is even and `2` when `n` is odd. The sum of the series is `(n/2) * (a1 + an/2)/2` as the sum of the progression.\nThis translates to:\n`(n/2)*(n-1 + 1)/2 => (n/2)*(n)/2` when even.\n`(n/2)*(n-1 + 2)/2 => (n/2)*(n+1)/2` when odd.\n\nWe can quickly check whether an integer is odd by a simple bit operation: `n & 1`. If `n` is even then the result is `0`, otherwise it\'s `1` because the LSB is `1` for odd numbers.\n\nThe solution is the following:\n\n```\nclass Solution {\n    public int minOperations(int n) {\n        return (n/2)*(n + (n & 1))/2;\n    }\n}\n```
[]