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.
>Solution :
You can solve the problem using an equation:

Apply this equation to your code. It will work perfectly with time complexity: O(1).