How max profit is computed

In the code discussed in the video, the code is giving the right result but I am not able to understand the intuition behind the code, what we are trying to do is either picking the item or leaving and checking the conditions based on that, but doing we are generating all possible profilts but then how maximum is resulting out, what step is ensuring comparison between all the iterations to get the maximum profit. max(inc, dec) is resulting the ans which is maxium between either taking the element or not taking it.(but how in all combinations it is giving the right result)

Hey @great
So first assume for 1 element
Ans=max(take a[0],leave a[0])

Now assume for 2 elements
Ans=max(A[1]+knapsack(1element) ,leave A[1]+knapsack(1element) )

See recursion of 2nd element involves recursion for 1 st element

And we can keep going like this

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.