Dsa
Prefix Sum
Warm-up
Daily Calorie Surplus
A fitness app tracks how many calories a user consumes daily. The user has a fixed daily calorie goal G. A day is defined as having a 'cumulative surplus' if the total calories consumed from the start of the log up to that day is strictly greater than the total goal for that same period. Given the goal G and a log of calories consumed for N days, count how many days had a cumulative surplus.
Input Format:
The first line contains two integers: N (the number of days) and G (the daily calorie goal).
The second line contains N integers, representing the calories consumed each day.
Output Format:
A single integer representing the number of days where the cumulative consumption exceeded the cumulative goal.
Example:
Input:
5 2000
2100 1900 2200 1800 2100
Output:
3
(Explanation: Day 1: 2100 > 2000 (Yes). Day 2: 4000 > 4000 (No). Day 3: 6200 > 6000 (Yes). Day 4: 8000 > 8000 (No). Day 5: 10100 > 10000 (Yes). Total = 3.)
Key concepts
prefix_sumlinear_scan
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now