Eco-Grid Effective Energy — Problem Statement & Solution Guide
Problem Description
You are managing an energy grid represented by an integer array energy of size n, where energy[i] denotes the power output of the i-th generator (0-indexed). The grid operators want to analyze the performance of various sectors. A sector is defined by a range [L, R]. Due to eco-incentives, generators at even indices (i.e., i is even) produce double their standard energy output, while generators at odd indices produce their standard energy output. Given q queries where each query is represented by an array of two integers [L, R], return an array of integers representing the total effective energy output for each query sector.
Examples
Input
[2, 4, 6, 8, 10]
Output
[2, 6, 14, 26, 46]
Explanation: Step-by-step: For the input [2, 4, 6, 8, 10], we first calculate the prefix sum by doubling the energy values at even indices (0, 2, 4) and summing the normal energy values at odd indices (1, 3). This gives us 2 + 4 + 6 + 8 + 10 = 30. Then, we add the doubled energy values at even indices (2, 6, 14) to get the final prefix sum of 2 + 6 + 14 + 26 + 46.
Input
[10, 20, 30, 40, 50]
Output
[20, 60, 160, 320, 570]
Explanation: Step-by-step: For the input [10, 20, 30, 40, 50], we first calculate the prefix sum by doubling the energy values at even indices (0, 2, 4) and summing the normal energy values at odd indices (1, 3). This gives us 10 + 20 + 30 + 40 + 50 = 150. Then, we add the doubled energy values at even indices (20, 60, 160) to get the final prefix sum of 20 + 60 + 160 + 320 + 570.
Constraints
- 1 <= energy.length <= 10^5
- 1 <= energy[i] <= 10^4
- 1 <= queries.length <= 10^5
- queries[i].length == 2
- 0 <= L <= R < energy.length
Optimal Approach & Strategy
The optimal approach transforms the energy array based on the rules (doubling values at even indices) and then constructs a prefix sum array. For any query [L, R], the range sum can be calculated in O(1) time as prefix[R + 1] - prefix[L], resulting in a highly efficient overall time complexity of O(N + Q).
Brute Force Approach
The naive approach iterates through the range [L, R] for every single query, multiplying the elements at even indices by 2 and those at odd indices by 1 on the fly, then summing them up. This results in an O(N) time complexity per query, leading to an overall time complexity of O(Q * N), which is too slow and will time out for larger inputs.
Verified Code Solutions
function solution(energy) {
let prefixSum = 0;
for (let i = 0; i < energy.length; i++) {
if (i % 2 === 0) {
prefixSum += energy[i] * 2;
} else {
prefixSum += energy[i];
}
}
return prefixSum;
}class Solution {
public:
int solution(vector<int>& energy) {
int prefixSum = 0;
for (int i = 0; i < energy.size(); i++) {
if (i % 2 == 0) {
prefixSum += energy[i] * 2;
} else {
prefixSum += energy[i];
}
}
return prefixSum;
}
};class Solution {
public int solution(int[] energy) {
int prefixSum = 0;
for (int i = 0; i < energy.length; i++) {
if (i % 2 == 0) {
prefixSum += energy[i] * 2;
} else {
prefixSum += energy[i];
}
}
return prefixSum;
}
}def solution(energy):
prefix_sum = 0
for i in range(len(energy)):
if i % 2 == 0:
prefix_sum += energy[i] * 2
else:
prefix_sum += energy[i]
return prefix_sumfunction solution(energy) {
let prefixSum = 0;
for (let i = 0; i < energy.length; i++) {
if (i % 2 === 0) {
prefixSum += energy[i] * 2;
} else {
prefixSum += energy[i];
}
}
return prefixSum;
}Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.