Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Summation of a abnormal complex series

In a recent cmpetitive programming contest, I faced a problem. I have found:

1 + (1 + 2) + (1 + 2 + 3) + ........... + (1 + 2 + .... + n) = ?

I tried using 2 nested loops:

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
        sum += j;
    }
}

But it is showing Time Limit Error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can solve the problem using an equation:
enter image description here
Apply this equation to your code. It will work perfectly with time complexity: O(1).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading