Dsa
Prefix Sum
Medium
Daily Temperature Change
Imagine you are tracking the daily temperature changes for a week. For each day, you are given the change in temperature compared to the previous day. You need to calculate the total temperature for a specific range of days. Write a function to return the sum of temperatures from day A to day B (both inclusive). Assume the temperature at day 1 is 0 degrees.
Input format:
- The first line contains an integer N (1 ≤ N ≤ 100).
- The second line contains N integers representing the temperature changes.
- The third line contains two integers A and B (1 ≤ A ≤ B ≤ N).
Output format:
- An integer representing the total temperature from day A to day B.
Example:
Input:
5
10 -5 15 -10 5
2 4
Output:
10
Key concepts
prefix_sumcumulative_sumtemperature
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