0-n knapsack not getting correct ans pls help

Hello @souravkhanna227, your logic is not correct. And int dp[w+1] = {0} this will only initialize first element equal to 0 and rest will be the garbage value. And try to do this in O(n^2) space it would be easy.
Please refer this and ask if you face any confusion.

sir in the hint video this is done with one dimentional array , how can we make that code as that will decrease our code time compolexity

for that yes we can use O(2*n) by maintaining a flag variable. But generally then also we say the time complexity is O(n^2), that doesn’t make the much difference, although yes it will reduce the space complexity a lot.