C++ AP Sum upto N numbers || O(1) Time || O(1) space || 100%⭐⭐⭐
Intuition\n Describe your first thoughts on how to solve this problem. \nAP Sum upto N numbers \n\n# Approach\n Describe your approach to solving the problem. \
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAP Sum upto N numbers \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n$$S(N)=N/2*(2*a+(N-1)*d)$$\n\nconsider odd n=7 (input)\n1 3 5 7 9 11 13\nwe can make all elements equal to 7 in minimum moves \n1 and 13 in 6 moves \n3 and 11 in 4 moves \n5 and 9 in 2 moves \n\nthis is an AP upto N numbers. (Here N will be given by n/2) where **a=2**\n\nconsider odd n=6 (input)\n1 3 5 7 9 11 \nwe can make all elements equal to 6 in minimum moves \n1 and 11 in 5 moves \n3 and 9 in 3 moves \n5 and 7 in 1 move \n\nthis is an AP upto N numbers. (Here N will be given by n/2) where **a=1**\n\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minOperations(int n) {\n        if(n==1) return 0;\n        int num=n/2;\n        int ans=0;\n        if(n%2==0){\n            int a=1;\n            ans=num*(a+(num-1));\n        }\n        else{\n             int a=2;\n             ans=num*(a+(num-1));\n        }\n        return ans;\n        \n    }\n};\n```
['C++']