This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 1: Arrays and strings
Precompute running totals once, answer any range in O(1).
Builds on Hash Maps & Sets.
The question to ask
Could I answer this from the difference between two running totals?
prefix[i] is the sum of everything before index i, so the sum of any range [l, r] is prefix[r + 1] - prefix[l]. That turns repeated range queries from O(n) each into O(1) each. Paired with a hash map of prefixes seen so far, it also counts subarrays with a given sum, and unlike a sliding window it still works when numbers are negative.
Time O(n) to build, O(1) per query · Space O(n)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
One extra slot at the front (prefix[0] = 0) means no special case for l = 0.
class NumArray {
private prefix: number[];
constructor(nums: number[]) {
this.prefix = [0];
for (const n of nums) this.prefix.push(this.prefix[this.prefix.length - 1] + n);
}
sumRange(left: number, right: number): number {
return this.prefix[right + 1] - this.prefix[left];
}
}Product of Array Except Self: answer[i] is the product to its left times the product to its right. One pass left to right, one right to left, no division.
function productExceptSelf(nums: number[]): number[] {
const answer = new Array<number>(nums.length).fill(1);
let left = 1;
for (let i = 0; i < nums.length; i++) {
answer[i] = left;
left *= nums[i];
}
let right = 1;
for (let i = nums.length - 1; i >= 0; i--) {
answer[i] *= right;
right *= nums[i];
}
return answer;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Return the running sum of an array, where each position holds the sum of all elements up to and including it.
"Running sum" is the definition of a prefix sum. The warm-up for everything else here.
Not attempted yet
Find the leftmost pivot index, where the sum of elements strictly to the left equals the sum of elements strictly to the right.
Left sum equals right sum: keep a running left sum; right is total - left - nums[i].
Not attempted yet
Design a class that answers multiple queries for the sum of the elements between two indices of a fixed array.
"Multiple queries" over "range sum" of data that never changes: precompute prefix sums once.
Not attempted yet
Return an array where each position holds the product of all the elements except the one there, in O(n), without using the division operator.
"Except self" plus "without using the division": prefix products from the left times suffix products from the right.
Not attempted yet
Return the total number of contiguous subarrays whose sum equals k. Numbers may be negative.
"Number of subarrays" whose "sum equals k" with negatives allowed: a window can't shrink reliably, so count prefixes in a map.
Not attempted yet
Return true if there is a subarray of length at least two whose sum is a multiple of k.
"Multiple of k": two prefixes with the same remainder mod k bracket a qualifying subarray. Map remainder to earliest index.
Not attempted yet
Answer multiple queries for the sum of elements inside a rectangle of a fixed matrix.
2D "range sum" over a "rectangle": a 2D prefix table and inclusion-exclusion (add the whole, subtract two strips, add back the corner).
Not attempted yet
A subarray is nice if it contains exactly k odd numbers. Return the number of nice subarrays.
"Exactly k" odd numbers: map each element to 1 if odd, and it becomes subarray sum equals k. Prefix counts again.
Not attempted yet