Python3 constant time O(1) & space
Basically we have an array with all the odd elements\n\nDepending upon the value of n we will have 2 different if cases :\n\n#### 1. If N is odd: \nThe value of
**Basically we have an array with all the odd elements**\n\nDepending upon the value of ```n``` we will have 2 different ```if``` cases :\n\n#### **1. If N is odd:** \nThe value of all elements is eventually going to be equal to the middle term (observation from notes taken on paper). Its ```True``` because there is equally larg subset of numbers that need to be compensated with ```+=1``` as with ```-=1```.\n\n(For instance for ```n=5 -> [1, 3, 5, 7, 9]``` eventually we will make the ```[5, 3, 5, 7, 5]```, hence 1 a 9 has been affected by the same number(4) ```k times```, so was 3 & 7 by different number(2) and just``` k-2 times```) We just need to try more values for ```n``` and for each sum all the compensation values. 4+2=6\n\nFor more n it is going to be the same, write down the observations and you will find yourself with something like me \n```\n1->0\n3->2\n5->6\n7->12\n9->20\n```\nWhich can be written as a\\*b where a+b=9 and a+1=b ```(5 -> 3*2 because 3+2=5) or (7 -> 4*3 because 4+3=7)...```\n\n\n#### **2. If N is even:** \nwe can make the same observation on paper:\n```\n2->1\n4->2\n6->9\n8->16\n10->25\n12->36\n```\nNow just we need to see there a pattern, this one is kinda obvious, the outputs are in form (n/2)\\*\\*2.\n\nWe have got ourselves solution in O(1) since we have been using just pure math to do the job. \n____\n## CODE in O(1)\n\n```\nimport math\n\ndef minOperations(n: int) -> int:\n\t\n\tif n%2==0:\n\t\treturn int((n/2)**2)\n\telse:\n\t\thalf=math.floor(n/2)\n\t\treturn half*(half+1)\n\n```\n
['Math', 'Python']