ITPEC FE Subject B October 2025 Question 3
ITPEC FE Subject B October 2025 — Question 3 of 20
The outer loop fixes the start index, the inner loop extends end from start to N, accumulating sum ← sum + arr[end] at each step.
- •Blank A = `N`: the inner loop must reach the last element, so
endruns fromstarttoN(1-indexed array of size N) - •Blank B = `sum + arr[end]`: each iteration adds the current element
arr[end]to the running sum to build the subarray sum incrementally
The early exit (elseif sum > targetSum → exit) is valid because all elements are positive integers — once the sum exceeds the target, extending further only increases it.
Why not others:
- arr[end] alone (no accumulation) — only checks single elements, not subarrays
- sum + arr[start] — adds the wrong element; start is fixed in the inner loop
- N - 1 as upper bound — skips the last array element
- targetSum as upper bound — meaningless as a loop index bound
Key rule: For subarray sum problems with positive integers, the sliding/expanding window works because sums are monotonically increasing as the window grows.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.