Exactly same thing i am doing using map y isnt it passing?


This is the code can you please check once.Its exactly same logic.Why is it wrong/

Hey @pranjalarora98
Here check for this

3
5 9 56

I hope u will understnad ur mistake incase u dont then let me know :slight_smile:

I didnt understand its coming 61 is it wrong? And what changes will i have to do if wrong.I dry ran also

Don’t you think it should be 70 because none of them are adjacent

dp[0]=0;
// map<int,int>::iterator itr=cust.begin(); not req
dp[1]=cust[1];//updated
int a=2;
// itr++; not req
while(a<=100000) //updated this
{
	dp[a]=max(dp[a-1],dp[a-2]+cust[a]*a);//updated this
	// itr++;//removed this no need
	a++;
}
cout<<dp[100000];//updated

Its not necessary that all the nos given are continous in that case u may be able to pick all the nos .

yeah understud i modified my code to

Actually now i am checking that whether a number minus that number exists or not
Like suppose i am on ith number so i am checking that whether i-1 is there or not and doing

Actually i was not liking that array method coz whats the need to create such a big array?

Yeah this is also good to go :slight_smile:

In worst case time complexity of this will be better than maps
O(n) in WC
and for Map
O(nlogn)

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.