why i am getting wrong answet for this problem?

I am getting wrong answer for this problem:
link text

it satisfies all test cases given in problem + few more

my solution:
link text

Your approach is wrong… It won’t satisfy all the test cases.

First of all you are using an O(n^2) sort that can cause you a TLE. (Sorting is not required though)

You can use the following two methods

1.) Brute Force

2.)Use dynamic programming such that dp[i][j]=1 when there is a sum of j possible up till the ith position.

So you will just have to see the value of dp[n][m].

Check this test case:


5 10

2 2 3 4 5

Your solution is giving “NO” while the answer should be “YES”. (2+3+5=10)

1 Like

Maximum value of n is 20. So, sorting would not cause TLE.