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.

63251173

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”.

[Program]
○ subArraySum(integer []: arr, integer: targetSum)
integer: sum, start, end
integer: N ← the number of elements in arr
for (increase start from 1 to N by 1)
sum ← 0
for (increase end from start to A by 1) // α
sum ← B
if (sum = targetSum)
output start, end
return
elseif (sum > targetSum)
exit the for block marked α
endif
endfor
endfor
output "No subarray found"

Answer group

OptionAB

Explanation

A: N, B: sum + arr[end] — for each start position, grow the subarray one element to the right while keeping a running total.

  • •end is the right edge of the subarray, an index, and it must be able to reach the last element: A is N.
  • •sum is reset to 0 for each start and must hold the total of arr[start] to arr[end], so each step adds the newly included element: B is sum + arr[end].
  • •Every element is positive, so once sum exceeds targetSum a 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] or arr[start] alone overwrites sum, 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 - 1 never lets end reach the last element, so any subarray ending at arr[N] is missed
  • •(g), (h) and (i) — targetSum bounds an index by a data value; when it exceeds N, the loop reads past arr[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.