Skip to content

CodeNot attempted

Subarray with a given sum (negatives allowed)

Difficulty: medium

Return the first contiguous subarray summing to target. Numbers can be negative, so a sliding window breaks. Trying every start is O(n²) and fine; bonus: prefix sums + a hash map make it O(n).

Show reference solution
// Try every start. From each start, keep adding numbers and check the sum. O(n²).
// A sliding window doesn't work here, because negative numbers can make the sum go down.
export function subarrayWithSum(nums: number[], target: number): number[] | null {
for (let start = 0; start < nums.length; start++) {
let sum = 0;
for (let end = start; end < nums.length; end++) {
sum += nums[end];
if (sum === target) {
return nums.slice(start, end + 1);
}
}
}
return null;
}
Loading editor…