Peak Energy Production Window

🎯 Level: Easy Score: 30 ⏱️ Estimated Time: 30 mins
📜 Problem Description

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.

Input Format

The first line contains an integer 'T' denoting the number of test cases.

For each test case, the input consists of two lines:

  • The first line contains two space-separated integers: 'n' (the total number of hourly readings in array A) and 'k' (the size of the production window).
  • The second line contains 'n' space-separated integers, representing the energy production readings (elements of array A).

Output Format

For each test case, output a single integer: the maximum total energy produced within any k-hour window.

Constraints

  • 1 <= T <= 100
  • 1 <= n <= 104
  • 1 <= k <= n
  • 1 <= Ai <= 104 (Each hourly reading will be a positive integer)
📌 Constraints
1 <= T <= 100; 1 <= n <= 10^4; 1 <= k <= n; 1 <= Ai<= 10^4
📝 Sample Input/Output
Sample Input:
2
7 3
3 5 6 2 4 7 8
6 1
1 3 3 3 4 4
Sample Output:
19
4
Explanation for Sample 1 (7 3):

Hourly Readings: [3, 5, 6, 2, 4, 7, 8], Window Size: k = 3

Let'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 = 19

The maximum sum among these windows is 19.

Explanation for Sample 2 (6 1):

Hourly Readings: [1, 3, 3, 3, 4, 4], Window Size: k = 1

When 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 = 4

The maximum sum is 4.

💡 Hints
  • 💡 Consider an approach that avoids re-calculating the sum for each new window from scratch. How can you efficiently update the sum as your window moves?
  • 💡 Think about the 'sliding window' technique. When the window slides one position to the right, which element leaves the window and which element enters?
  • 💡 Keep track of the current sum within the window and a separate variable for the maximum sum encountered so far.
📚 Topics

Two Pointers

🏢 Asked By Companies

Apple, Facebook, Ola, Oyo

😕 No submissions found

Start coding and your submissions will appear here.