ITPEC FE Subject B October 2025 Question 3
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.
Given an array of positive integers and a target sum, the problem is to find the subarray whose sum is equal to the target sum. A subarray is a part of the given array composed of contiguous elements. For example, when the target sum is 14, the shaded subarray in the figure satisfies the given condition.
Figure Example of an array
The procedure subArraySum receives an integer array arr and an integer value targetSum as arguments and finds the subarray whose sum is equal to the target sum. If a subarray that satisfies this condition is found, it prints their starting and ending indices, and finishes the task. Otherwise, it prints the message “No subarray found”.
Answer group
Explanation
A: N, B: sum + arr[end] — for each start position, grow the subarray one element to the right while keeping a running total.
- •
endis the right edge of the subarray, an index, and it must be able to reach the last element: A isN. - •
sumis reset to 0 for eachstartand must hold the total ofarr[start]toarr[end], so each step adds the newly included element: B issum + arr[end]. - •Every element is positive, so once
sumexceedstargetSuma longer subarray can only be larger, and the α loop is abandoned.
Trace for {6, 3, 2, 5, 1, 1, 7, 3} with target 14:
- •
start = 1: 6, 9, 11, 16, over 14, exit - •
start = 2: 3, 5, 10, 11, 12, 19, exit - •
start = 3: 2, 7, 8, 9, 16, exit - •
start = 4: 5, 6, 7, 14, equal, output 4, 7
Answer (b)
Why not others:
- •(a), (d) and (g) —
arr[end]orarr[start]alone overwritessum, so only single elements are ever tested; for the example no element equals 14 and "No subarray found" is printed - •(c), (f) and (i) —
sum + arr[start]adds the same first element again and again; for the example (c) outputs 7, 8, because 7 + 7 = 14 - •(d), (e) and (f) —
N - 1never letsendreach the last element, so any subarray ending atarr[N]is missed - •(g), (h) and (i) —
targetSumbounds an index by a data value; when it exceedsN, the loop reads pastarr[N]
Key rule: For a running subarray sum, reset the total when the left edge moves and add the new element as the right edge moves; an index bound must come from the array size, not from the data.
Array Manipulation · AI-written, may contain errors
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.