Quote:
I need help to solve this programming question
The problem comes from a challenge site, this means that you need to be smart, and
a brute force program is never the solution. You need to find ways to avoid unnecessary work, and/or find ways to reduce work.
Quote:
But I need more optimized solution with explanation. please help.
The problem is that your code is so brute force that it need a complete rewrite.
First of all, you need to understand the logic of the solution:
Try this by hand:
2
3 2 2
3 1 5
3 3 2
3 1 5
Then
2
3 1000 2
3 1 5
3 10000 2
3 1 5
Do you need to copy 1000 times the sequence and check every single possible sequence ?
Or you can find a better way?
'Better ways' is your optimization.
Every single sequence is a triangular number, it means that for a sequence of 3000, number of subsequences is 3000*3001/2= 4501500 subsequences.
Triangular number - Wikipedia[
^]