C++ Maths 1 liner w Explanation
We have an array of n sequential odd numbers starting from 1. It is fairly simple to prove (induction) that the sum of the first n odd numbers is just n*n. So t
We have an array of `n` sequential odd numbers starting from 1. It is fairly simple to prove (induction) that the sum of the first `n` odd numbers is just `n*n`. So the value that is closest to each term is the average, i.e. `sum([1, 3, 5, ... ]) / n = n*n / n = n`.\nNow we need to apply the operations. As the first and last terms are an equal distance from the average we can pair up terms and only consider the distance of each from the average. Note that if there is an odd number of terms, the middle term must be the average, and hence has 0 operations applied to it. Now if we apply the operations we see that we have `n - (2*i + 1)` (number of ops to make `ith` term equal to `n`) operations applied to the `ith` term, as there are `n/2` terms we can separate the sum into `n*(n/2) - (1 + 3 + 5 + ... ) = n*n/2 - (sum of n/2 sequential odd numbers) = n*n/2 - n*n/4 = n*n/4`.\n```\nclass Solution {\npublic:\n    int minOperations(int n) {\n      return n*n / 4;\n    }\n};\n```
['Math', 'C']