# Diet Plan Performance in Python

PythonServer Side ProgrammingProgramming

Suppose a dieter consumes calories[i], this indicates the calories on the i-th day. If we have an integer k, for every consecutive sequence of k days that is (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they find T. The T is the total calories consumed during that sequence of k days (calories[i] + calories[i+1] + ... + calories[i+k-1]): But there are some conditions;

• If T < lower bound, they performed poorly on their diet so lose 1 point;
• If T > upper bound, they performed well on their diet so gain 1 point;
• Otherwise, they performed normally. So points are the same.

At first, the dieter has zero points. We have to find the total number of points the dieter has gained.

If the array is [6,5,0,0] and k = 2 and lower := 1 and upper := 5, then the output will be 0. C + C > upper, so increase one point, lower <= C + C <= upper so no change, after that C + C < lower, so decrease one point, so the output will be 0.

To solve this, we will follow these steps −

• temp := 0
• for i in range 0 to k
• temp := temp + C[k]
• right := k – 1, and left := 0, and points := 0
• while right < length of C, do
• if temp < lower, then decrease points by 1, if temp > upper, then increase points by 1
• temp := temp – C[left]
• increase left and right by 1
• if right >= length of C, then come out from the loop
• temp := temp + C[right]
• return points

## Example

Let us see the following implementation to get better understanding −

Live Demo

class Solution(object):
def dietPlanPerformance(self, c, k, l, u):
temp = 0
for i in range(k):
temp += c[i]
right = k-1
left = 0
points = 0
while right < len(c):
if temp<l:
points-=1
elif temp>u:
points+=1
temp -=c[left]
left+=1
right+=1
if(right >= len(c)):
break
temp+=c[right]
return points
ob1 = Solution()
print(ob1.dietPlanPerformance([6,5,0,0],2,1,5))

## Input

[6,5,0,0]
2
1
5

## Output

0