370. Range Addition
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to perform a series of range update operations on an array that is initially all zeros. A single update operation involves adding a value inc to all elements within a given range [start, end]. A naive approach would be to iterate through each update and then iterate through the specified range to add the increment. If there are k updates and the array has length n, this approach would have a time complexity of O(k * n) in the worst case (when ranges are large), which is too slow given the constraints.
Solution
Range Addition in Python
class Solution:
def getModifiedArray(self, length: int, updates: list[list[int]]) -> list[int]:
# Create a result list of size 'length', initialized to all zeros.
# This will act as our difference array.
result = [0] * length
# Apply each update operation on the difference array.
for start_idx, end_idx, inc in updates:
# Add the increment at the start index.
result[start_idx] += inc
# Subtract the increment at the index right after the end index.
# This cancels out the effect of 'inc' for all subsequent elements.
if end_idx + 1 < length:
result[end_idx + 1] -= inc
# Calculate the prefix sum to get the final state of the array.
# The value at each index is the sum of all changes up to that index.
# This can be done in-place.
for i in range(1, length):
result[i] += result[i - 1]
return result
Range Addition in Java
class Solution {
public int[] getModifiedArray(int length, int[][] updates) {
// Create a result array of size 'length', initialized to all zeros.
// This will act as our difference array.
int[] result = new int[length];
// Apply each update operation on the difference array.
for (int[] update : updates) {
int startIdx = update[0];
int endIdx = update[1];
int inc = update[2];
// Add the increment at the start index.
result[startIdx] += inc;
// Subtract the increment at the index right after the end index.
// This cancels out the effect of 'inc' for all subsequent elements.
if (endIdx + 1 < length) {
result[endIdx + 1] -= inc;
}
}
// Calculate the prefix sum to get the final state of the array.
// The value at each index is the sum of all changes up to that index.
for (int i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
}
}
Range Addition in C++
class Solution {
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
// Create a result vector of size 'length', initialized to all zeros.
// This will act as our difference array.
vector<int> result(length, 0);
// Apply each update operation on the difference array.
for (const auto& update : updates) {
int startIdx = update[0];
int endIdx = update[1];
int inc = update[2];
// Add the increment at the start index.
result[startIdx] += inc;
// Subtract the increment at the index right after the end index.
// This cancels out the effect of 'inc' for all subsequent elements.
if (endIdx + 1 < length) {
result[endIdx + 1] -= inc;
}
}
// Calculate the prefix sum to get the final state of the array.
// The value at each index is the sum of all changes up to that index.
for (int i = 1; i < length; ++i) {
result[i] += result[i - 1];
}
return result;
}
};
Range Addition in TypeScript
function getModifiedArray(length: number, updates: number[][]): number[] {
// Create a result array of size 'length', initialized to all zeros.
// This will act as our difference array.
const result = new Array(length).fill(0);
// Apply each update operation on the difference array.
for (const update of updates) {
const [startIdx, endIdx, inc] = update;
// Add the increment at the start index.
result[startIdx] += inc;
// Subtract the increment at the index right after the end index.
// This cancels out the effect of 'inc' for all subsequent elements.
if (endIdx + 1 < length) {
result[endIdx + 1] -= inc;
}
}
// Calculate the prefix sum to get the final state of the array.
// The value at each index is the sum of all changes up to that index.
for (let i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.