If the input is 1 2 3 3 4 5
, the subsequences are:
1
1 2
1 2 3
1 2 3 3
1 2 3 3 4
1 2 3 3 4 5
2
2 3
2 3 3
...
Over all the subsequences, 1 2 3 3
is such that |(1 + 2 + 3 + 3) - (4 + 5)| = 0
is minimal.
If we take the subsequence 2 3 3
, we have |(2 + 3 + 3) - (1 + 4 + 5)| = |8 - 10| = 2
which is greater.
However, I'm also confused by the meaning of "subsequence". I thought that, the subsequences of 1 2 3 4 5
should be:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2
2 3
2 3 4
2 3 4 5
3
3 4
3 4 5
4
4 5
5
But according to the subject, the optimal subsequence is 1 2 4
which is not in my list. It's in fact a subset of the set S
. So be careful, there are many combinations.