Please check code for The subset to sum Target problem
hello @sahilkhan2312000131
ur dp states are wrong.
both sum and i are changing in the the recusive relation thats why we need 2 dp to uniquely identify the state.
check now->
now try to solve this in O(SUM) space
if(n==0 ) return sum==0;
What will it do?
What is the problem with if(n==0 and sum!=0) return 0;
this will handle two cases
if(n==0 && sum==0) return 1
if(n==0 && sum!=0) return 0;
u missed the first condition that i mentioned above,becuase of that furhtur statement will be executed and becuase n is 0 , n-1 will be -1 , ur program will try to access negative index becuase of that u will get segmentation fault
see when we make array global then in that case the memory is allocated over data segement but when we declar3e array inside function then it get allocated over stack .
data segment has comparatively higher memory capacity then stack thats why the code that i shared is working(becuase i made it global)
and the code that u r trying is showing run error(because u have declared array inside function)
roughly inside function u can declare upto 10^6…10^7 size array
make ur array global and it will work
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.