C++ | O(1) | 2 line solution - Explained in detail - Finding the nth term
Figuring out the general term of the series can be tricky.\nThe key here is to focus on the number of final operations required.\n\nStarting with n=1, the first
Figuring out the general term of the series can be tricky.\nThe key here is to focus on the number of final operations required.\n\nStarting with `n`=1, the first few terms of the series would be:\n0, 1, 2, 4, 6, 9, 12, 16, 20, 25 and so on... \n\nIf you see the difference between 2 consecutive terms, now you begin to see a pattern:\n1, 1, 2, 2, 3, 3, 4, 4 and so on...  \n\nWe separate the odd and even terms and get the 2 new series:\nFor `n%2==0` we get 1, 4, 9, 16, 25...\nFor `n%2==1` we get 0, 2, 6, 12, 20...\n\nNow, it is clear that for all the even numbers, we have to return the square of `n/2` \nFor odd, simply return `n/2` multiplied by the next consecutive number `n/2 +1`\n\nFeel free to optimize further or mention any different approach of handling this problem :)\n```\nclass Solution {\npublic:\n    int minOperations(int n) {\n        if (n%2==0) return (n/2)*(n/2);\n        else return (n/2)*(n/2 +1);\n    }\n};\n```\nPlease upvote if you liked this approach... Thanks!
['Math', 'C']