Doubt in a question of hackerblock

can anyone provide solution to the IPL problem in hackerblocks i am unable to solve it

hello @simi_1188

Input: arr[]={1,2,3} Output: 5 We can’t take three of them, so answer is 2+3=5

Input: arr[]={3000,2000,1000,3,10} Output: 5013

Input: arr[]={100,1000,100,1000,1} Output: 2101 100+1000+1000+1=2101

sum[i] : Stores result for subarray arr[0…i], i.e.,maximum possible sum in subarray arr[0…i] such that no three elements are consecutive. sum[0] = arr[0] Note : All elements are positive sum[1] = arr[0] + arr[1] We have three cases 1) Exclude arr[2], i.e., sum[2] = sum[1] 2) Exclude arr[1], i.e., sum[2] = sum[0] + arr[2] 3) Exclude arr[0], i.e., sum[2] = arr[1] + arr[2] sum[2] = max(sum[1], arr[0] + arr[2], arr[1] + arr[2])

In general, We have three cases 1) Exclude arr[i], i.e., sum[i] = sum[i-1] 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i] 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] + arr[i-1] sum[i] = max(sum[i-1], sum[i-2] + arr[i], sum[i-3] + arr[i] + arr[i-1])

@simi_1188
do u still have any doubt ?

no everything clear thanks

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.