A leading renewable energy firm, 'SunHarvest Dynamics', is intensely focused on optimizing its solar farm operations. To better understand the performance of their solar panels, they collect hourly energy production readings. These readings fluctuate throughout the day and across seasons.
Your task is to help 'SunHarvest Dynamics' identify the period of k consecutive hours that yielded the maximum total energy production. By finding these peak production windows, they can make informed decisions about energy storage, grid distribution, and maintenance schedules.
Given an array A representing hourly energy production readings and an integer k representing the duration of the time window in hours, find the maximum sum of energy produced within any contiguous window of size k.
The first line contains an integer 'T' denoting the number of test cases.
For each test case, the input consists of two lines:
A) and 'k' (the size of the production window).A).For each test case, output a single integer: the maximum total energy produced within any k-hour window.
1 <= T <= 1001 <= n <= 1041 <= k <= n1 <= Ai <= 104 (Each hourly reading will be a positive integer)1 <= T <= 100; 1 <= n <= 10^4; 1 <= k <= n; 1 <= Ai<= 10^4
Sample Input:
2 7 3 3 5 6 2 4 7 8 6 1 1 3 3 3 4 4Sample Output:
19 4Explanation for Sample 1 (7 3):
Hourly Readings:
[3, 5, 6, 2, 4, 7, 8], Window Size:k = 3Let's examine all possible 3-hour windows and their total energy production:
- Window 1:
[3, 5, 6]→ Sum = 3 + 5 + 6 = 14- Window 2:
[5, 6, 2]→ Sum = 5 + 6 + 2 = 13- Window 3:
[6, 2, 4]→ Sum = 6 + 2 + 4 = 12- Window 4:
[2, 4, 7]→ Sum = 2 + 4 + 7 = 13- Window 5:
[4, 7, 8]→ Sum = 4 + 7 + 8 = 19The maximum sum among these windows is 19.
Explanation for Sample 2 (6 1):
Hourly Readings:
[1, 3, 3, 3, 4, 4], Window Size:k = 1When
k = 1, we are simply looking for the single highest hourly reading.
- Window 1:
[1]→ Sum = 1- Window 2:
[3]→ Sum = 3- Window 3:
[3]→ Sum = 3- Window 4:
[3]→ Sum = 3- Window 5:
[4]→ Sum = 4- Window 6:
[4]→ Sum = 4The maximum sum is 4.
Two Pointers
Apple, Facebook, Ola, Oyo
Start coding and your submissions will appear here.