[JAVA] O(1) Constant time solution with detailed explanation.
Basic Approach:\n            if n is odd, suppose n=5.\n                The array is :\n                1 3 5 7 9.\n                Here, we will have the middl
Basic Approach:\n            if n is odd, suppose n=5.\n                The array is :\n                1 3 5 7 9.\n                Here, we will have the middle element as 5. \n                We take 2 from 7 and add to 3 to make each one 5.\n                We take 4 from 9 and add to 1 to make each one 5.\n                Total steps: 2+4=6. (sum of first n/2 even numbers)  \n\t\t\t\tSum of first k EVEN numbers = \n\t\t\t\tSum(i=1 to k) {2 * i} = 2 * ( Sum(i=1 to k){ i } )\n\t\t\t\t\t\t\t\t\t\t\t\t= 2 * (k*(k+1))/2\n\t\t\t\t\t\t\t\t\t\t\t\t= k*(k+1)\n           if n is even, suppose n=6.\n                The array is :\n                1 3 5 7 9 11.\n                Here, we will have the middle element as (5+7)/2=6.\n                We take 1 from 7 and add to 5 to make each one 6.\n                We take 3 from 9 and add to 3 to make each one 6.\n                We take 5 from 11 and add to 1 to make each one 6.\n                Total steps: 1+3+5=9. (sum of first n/2 odd numbers) \n\t\t\t\tSum of first k ODD numbers = k * k.\n```\nclass Solution {\n    public int minOperations(int n) {\n\t// Take care of overflow if n is too large.\n        if(n%2==1){\n            n/=2;\n            return (n*(n+1));\n        }        \n        n/=2;\n        return n*n;\n    }\n}\n```\nThanks for reading. Please share and upvote.
[]