C++ | Easy O(1) Mathematical Solution with intuition | 1 line code
The Array is [ 1 3 5 7 9 11 13 15....... (2n-1) ]\nNow,\n\tLets take n=7 (odd)\n\tArray= [ 1 3 5 7 9 11 13 ]\nNow we will equalise the values in such a way that
The Array is [ 1 3 5 7 9 11 13 15....... (2n-1) ]\nNow,\n\tLets take n=7 (odd)\n\tArray= [ 1 3 5 7 9 11 13 ]\nNow we will **equalise** the values in such a way that the larger numbers and smaller numbers converge to a common number. So we start with the extremes and left extreme is increased by 1 and right extreme is decreased by 1. the **converging numbers is median of the array** (here 7 which is equal to n) by observation and some examples.\n\t[ 2 3 5 7 9 11 12 ] - 1\n\t[ 3 3 5 7 9 11 11 ] - 2\n\t[ 4 3 5 7 9 11 10 ] - 3\n\t[ 5 3 5 7 9 11 9 ] - 4\n\t[ 6 3 5 7 9 11 8 ] - 5\n\t[ 7 3 5 7 9 11 7 ] - 6\n\tnow we shift to next extremes and repeat the steps\n\t[ 7 4 5 7 9 10 7 ] - 7\n\t[ 7 5 5 7 9 9 7 ] - 8\n\t[ 7 6 5 7 9 8 7 ] - 9\n\t[ 7 7 5 7 9 7 7 ] - 10\n\t[ 7 7 6 7 8 7 7 ] - 11\n\t[ 7 7 7 7 7 7 7 ] - 12\n\t\nTotal 12 operations\n\t\nSimilarly we can do for the even n.\nWe observe that we increased/decreased the repective extremes one by one by **(n - their value)** times. So **total number of operations** will be will be **(n - their value) times for n/2 numbers exactly** as we increase and decrease in same operation.\n\nSo here n=7=median(common)\nans=(n-1)+(n-3)+(n-5)=6+4+2=12   { n+n+n - (1+3+5)}\nNow calculation can be generalised as sum of **Arithmetic progression**\nsum=n/2[2a+(n-1)d] {where d=2,a=1,n=n/2}\nThis sum needs to be subtracted from n+n+n.....n/2 times=n * n / 2\nFinal ans=n * n/2 - sum which simplifies as (n * n)/4.\n\n```\nint minOperations(int n) {\n         return (n*n)/4;   \n    }\n```\nPlease Upvote if you find it helpful. \n\t
['Array', 'Math', 'C']