Explanation in desccription

What method i am thinking for this is to make a map and then store all elements with frequency.So when i choose ith element
I will have i*freq(i) points right? And then freq of i-1 and i+1 element will be set to 0.And after this?

hello @pranjalarora98
yeah u r on right track.
first store frequency in map say (freq) and then remove duplicates and then sort array after that.
dp[i] = max of( when include ith element or when exclude ith element ). considering only [0…i]

if u exlude ith element then dp[i-1]

if u include ith element then a[i] * freq[a[i]] +( dp[i-2] if a[i-1] +1 ==a[i]) otherwise
a[i]*freq[a[i]] + dp[i-1]

take max of both the cases

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.