# Maximum Calorie

Maximum calorie: In this article, we are going to describe a standard dynamic programming problem. It can be featured in any interview problem round.
Submitted by Radib Kar, on June 08, 2020

Problem statement:

Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since Shivang is very foodie, so he decided to take as much as calorie while remembering the fact that he cannot eat on three consecutive days. Help him in finding the number of calories he could take.

Input:

n i.e number of days. In the next line is n space-separated values of ai indicating the intake of calorie per day.

Output:

Print the maximum amount of calorie, Shivang can take in a new line.

Constraints:

```1<=n<=100000
1<=ai<=100000
```

Example:

```Input:
2
n=5
Array input, calorie for each day:
3 2 1 10 3

Output:
18

Explanation:
Shivang will eat on 1st, 2nd, 4th and 5th day
So, total he intakes 18 which is maximum

Input:
8
3 2 3 2 3 5 1 3

Output:
17

Explanation:
Shivang will eat on 1st, 3rd, 5th , 6th and 8th
day which totals (3+3+3+5+3) = 17
```

Solution Approach:

Let say,

```f(n) = maximum calorie can be taken till nth day under the constraint
```

No, let's think of the case,

1. If Shivang takes food on ith day, he need to skip on (i-1)th day and sum with f(i-2)
2. Tale food on ith day, (i-1)th day and skip on (i-2)th day, sum up with f(i-3)
3. Skip ith day, so take f(i-1)

So,

```f(i)=maximum(f(i-1),f(i-2)+arr[i],arr[i]+arr[i-1]+f(i-3) i>=3

For base case,
f(0)=arr
f(1)=arr+arr
f(2)=maximum(arr+arr,arr+arr,arr+arr)
```

Converting to Dynamic programming

Now, we need to convert the above recursion into dynamic programming to avoid the overlapping sub-problem re-computation

```1)  Declare int DP[n];
2)  Fill with the base case
DP=arr;
DP=arr+arr;
DP=std::max(arr+arr,std::max(arr+arr,DP));
3)  Compute the other values
for i=3 to n-1
DP[i]=maximum(arr[i]+arr[i-1]+DP[i-3],arr[i]+DP[i-2],DP[i-1]);
end for
4)  Return the result, DP[n-1];
```

C++ Implementation:

```#include <bits/stdc++.h>
using namespace std;

int maximumCalorie(vector<int> arr, int n)
{

int DP[n];
// base case
DP = arr;
DP = arr + arr;
DP = std::max(arr + arr, std::max(arr + arr, DP));

for (int i = 3; i < n; i++) {
DP[i] = std::max(arr[i] + arr[i - 1] + DP[i - 3], std::max(arr[i] + DP[i - 2], DP[i - 1]));
}

return DP[n - 1];
}

int main()
{
int t, n, item;

cout << "Enter number of days\n";
cin >> n;
cout << "Enter calories\n";
vector<int> a;

for (int j = 0; j < n; j++) {
scanf("%d", &item);
a.push_back(item);
}

cout << "Maximum calorie sudhir can take: " << maximumCalorie(a, n) << endl;

return 0;
}
```

Output:

```Enter number of days:
8
Enter calories:
3 2 3 2 3 5 1 3
Maximum calorie sudhir can take: 17
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7704653,"math_prob":0.9913698,"size":2978,"snap":"2021-43-2021-49","text_gpt3_token_len":997,"char_repetition_ratio":0.11600538,"word_repetition_ratio":0.024242423,"special_character_ratio":0.35224983,"punctuation_ratio":0.15469614,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9947765,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-28T04:30:27Z\",\"WARC-Record-ID\":\"<urn:uuid:d73c262d-8963-4f9f-9977-d36f38d8b0a5>\",\"Content-Length\":\"132334\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ebcf1765-2af5-477c-ac48-341dca688f42>\",\"WARC-Concurrent-To\":\"<urn:uuid:e8e17b71-1524-477a-b133-81692fffa7f7>\",\"WARC-IP-Address\":\"104.21.9.18\",\"WARC-Target-URI\":\"https://www.includehelp.com/icp/maximum-calorie.aspx\",\"WARC-Payload-Digest\":\"sha1:ANKQ5UB4F3Z6RE2EQYLEA7UREG75CBZS\",\"WARC-Block-Digest\":\"sha1:IC6Q5V36UXDC65KD2HO3ZUZLJGG6Y7WW\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323588257.34_warc_CC-MAIN-20211028034828-20211028064828-00139.warc.gz\"}"}